You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Make it possible to have "internal" state with or without using classes
Reduce Redux boilerplate for properties like UI flags
Easily allow multiple instances of the same component to get their own uniquestate
Get the benefits of Redux without most of the work for UI state
How it works
Create a config object with an optional name property and a function (whichgets passed props) which returns a state object consisting of your initialstate.
Wrap your component in the HOC.
An action gets dispatched likeUI_STATE_ADD:some-component_<uuid> oncomponentWillMount
The initial state gets added to auiState slice of the store with thename/state.
CallsetUiState with a new state object.
An action gets dispatched likeUI_STATE_SET:some-component_<uuid>
Component state gets removed from store oncomponentWillUnmount (optional)
setUiState: Simple update function that accepts an object that will be shallowmerged with current state. Accepts an optional callback called with the statechange.
resetUiState: Function to reset component to initial state. Accepts anoptional callback called with the initial state.
uiState Options
name {String | Function}
If a string is provided a uuid will be suffixed in order not to overwrite thestate in the store. Providing a function gives you the ability to create yourown unique name, with the ability to use the component'sprops, e.g:
There is a simple example app made withcreate-react-app in the./examplefolder. Simplecd into that directory,yarn install && yarn start. You canedit theApp.js file to add more instances of theButton andTextcomponents.
Feel free to open a PR!
About
React higher order component to provide UI state for components.