React State Libraries
State Libraries
What is a state library?
A state library helps you manage and share data (state) in your application.
-
State = data that changes over time. Example: user input, shopping cart items, whether a modal is open, etc.
-
A state library gives you tools to:
- Store the state
- Update it
- Reactively update your UI when the state changes
- Often, also helps share state between many components
React State Library : Redux
1. Install Redux & React-Redux
npm install redux react-redux
2. Create your Redux store
3. Setup Provider in your App
Wrap your app with <Provider>
to give Redux store access to all components.
4. Use Redux state & dispatch in components
- useSelector to read state
- useDispatch to send actions
- When you click +1 or -1, it dispatches an action to Redux.
- Redux reducer updates the state.
- useSelector re-renders your component with the new count.