Redux In Easy Steps
Redux is a predictable state container for JavaScript apps. … You can use Redux together with React, or with any other view library. It is tiny (2kB, including dependencies). In short, Redux allows you to manage state for your web applications built in any JavaScript framework such as React, Meteor, or Angular.
Getting Started
Create a react app and install redux:
$ npx create-react-app learn-redux
then go in your new folder and run $ npm install redux react-redux
Redux is the state management package and react-redux give us the ability to connect react and redux together. We will step up redux in 4 easy steps.
- Reducer
Basically the reducer is how your action transform the state to the next state and it modify it.
Create a reducers folder and files where you specify your actions that will change your state .
Let’s create a counter reducers that will increment or decrement on click and isLogged ( a boolean) that will change the state to false to true.
-src
-reducers
-counter
-isLogged
The first argument is your state that is set to 0 and the second argument is your action where you call the action.type and will switch between cases .
Same for the isLogged a state set to false and an action that will be called with action.type and switch between cases.
Last thing in the reducers folder, create an index.js to import all our reducer, it will help us to import all the reducer at once in App.js with a tool from redux called combineReducer .
- Store
It’s a Globalized state where we keep all the data(state) of our app and that is accessible everywhere in the app.
Let’s go back in the main index.js and import 2 things:
import {createStore) from ‘redux’ & import allReducer from ‘./reducers’
Then we set a store variable to createStore and in the parentheses allReducer. To visual our state we can import a nice extension from chrome Redux devTools . and need to add a few line in your store variable.
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
Last thing we want to have access to our state in the entire app and we can wrap our <app> with provider and we are all set!!!
- Accessing your state
Now we have set our store and reducers we can have access to our state everywhere. We just need to import {useSelector} from ‘react-redux’ and create a variable set to a function useSelector and specify with state we want to access.
Same thing if we want to use isLogged
- Action / Dispatch
We have access to our state but how can we change it ?? here Action comes and action literally make an action to your reducer and modify your state. Create an actions folder in src and an index.js
Then we import our action and useDispatch from ‘react-redux’ that give us the ability to dispatch an action.
We set dispatch variable to useDispatch() and onClick a call back function with dispatch and in the parentheses increment.
Same process for Decrement we create a new function in our actions/index.js
Last thing if we want to increment by 5 instead of one we can pass 5 as an argument and make few change in actions and reducers.
in actions/index.js we add an argument and a new key (usually called payload) with the argument
and Finally in our Reducer we switch +1 to action.playload where about it s equal to our argument
Redux requires some setup but when it’s done and with some practice managing your states will be so easier.