cleaner state management for react without pulling any dependency
this is not a package, it is a micro implementation of the approach/ idea. you can think it of a clean alternative to useState for individual component's state management without needing to install any package.
Feel free to download the app & check the source to get the idea, in the project directory, run:
this approach uses react's useReducer hook and a custom HOC thats it.
the HOC wraps your component just like Redux does and you get state and action as props for that component
- ✔ create
actionTypeconstants - actionType.js - ✔ create
reducer- post.js - ✔ create
action.jswhere you keep all the functions (as object) which are capable of dispatching to reducers (any extra works before dispatching also can be done here, but dont forget to useawaitstatements properly ) - action.js - ✔ export your component using the
withActionHOC - PostsComponent.js - ✔ now you can get the state by using
props.statePostsComponent.js - ✔ apply any action by using
props.action.ACTION_PROPERTY_NAME- PostsComponent.js
🚫 this approach is for state management of individual component not for entire app so don't think it as a replacement of Redux
🚫 make seperate reducer & action for individual components and don't use any other component's reducer in a different component's export thinking that you'll get its state outside of the component ( this approach uses useReducer & that's how it works )
-
only go for this approach when you think your component is using too many
useState -
you can use
useContextand apply the same idea of this approach to achieve the state management for your entire app without needing to install any other package