Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 23, 2022 04:34 pm GMT

State Management in React.Js

This week, I got myself playing around with React Hooks to grasp the underlying concepts on how it works, pros and tradeoffs.

I developed a simple scoreboard mini app for Liverpool FC versus Manchester United. Great idea right?
Here are some highlights on how it went down.

useState
useState

useState 2

useReducer
useReducer

Context API + useState
Context API + useState

Context API + useState

Context API + useState

Context API + useReducer
Context API + useReducer

Context API + useReducer
My takeaway from this exercise is that context API gives a high level abstraction and make state accessible to parent component and its children leveraging DRY principle compared to useState and useReducer been implemented with the same logic in multiple components that need the state.

useState and useReducer can be used independently of context API yet it fits perfectly into context API as state management grows.
It really an overkill to implement useReducer when all you have to mutate is a singular state value and also considering its verbose boilerplate. useReducer comes handy when an action mutate multiple state value e.g. in a cart system when you add an item to the cart - item(s) count, item(s) in cart, total cost of item(s) in cart, and possibly more depending on complexity.

Thank you reading. what's your thought on this?

PS: I'll be adding redux and its additives to this post soon. Watch out for this space


Original Link: https://dev.to/omohemma/state-management-in-reactjs-49m2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To