use-container
use-container
is a simpe hook to help you manage your global and local states without any need for configuration and no dependency on context.
How to use it
In order to use use-container
, you need to create a class that extends Container
, provided on the package.
interface User name: string email: string phoneNumber: string <User> public state = name: '' email: '' phoneNumber: '' public this public this public this // ...
Once you have your container, you can now start sharing its state:
const App = { const user = React return <input value=userstatename onChange= user />}
Share globally and locally
If your intention is to share the state globally, you can then use simply the reference to the class inside the useContainer
call. However, you can create local states by creating instances of those classes.
const App = { // uses the global state for UserContainer const user = return // ... } const App = { // creates a local state for UserContainer constlocalUser = React const user = return // ... }
If your state should be exclusively local, and you want to make sure it cease to exist after your component unmounts, you can pass a second parameter to the useContainer
hook, to delete the created container on unmount.
const App = { constlocalUser = React const user = // delete container on unmount return // ... }