Mastering State Management: A Deep Dive into Redux with React.js

Silas Adegoke
3 min readMay 24, 2024

--

Redux is a predictable state container for javaScript applications. It is used to manage state throughout the whole application especially when a data in a child component is needed in the parent component, it can be used instead of (useContext + useReducer) and useState. It was created to solve the issue of a state in a child componenet not being available in the parent component or in a sibling. Redux is not tied to React and can be used with Angular, Vue and Vanila javaScript. If you don’t know what state means, it means the internal data that a component can maintain and update over time, Redux creates and helps in managing a global state where the state is availabe throughout the whole application

Redux is used when the app state is updated fequently over time, the logic to update a state gets too comolex to track, the app has a large and complex sized codebase and is worked on by so many people

React-Redux was created to provide a seemless integration between ReactJs and Redux, it simplifies the connection of React components to the Redux store.

Installation

To install redux and react-redux in your react component, type the line below in your terminal:

npm i redux react-redux

In general there are 5 parts of a Redux:

  1. The state: The state is an object that contains the data you want to manage for example, let’s say you are building a rock-paper-scissors game, your state can be
initialState= {
type_ofGame: "Rock"
}

2. The Action: This describes what happened or what need to happen. It is a function that returns an object based on how you want to update the state. An example will still be on the rock-paper-scissors-game; an action for playing a rock can be defined as:

type ActionType={
type: string
}
export function play_rock(): ActionType{
return{
type:"ROCK"
}
}

3. The Reducer: This is a function that updates the initial state based on the action that is performed. It is what ties the store and action together. Still on the rock-paper-scissors; a reducer can be defined as:

export function GameReducer(state=initialState, action:{type:string}){
switch(action.type){
case ROCK:return{
...state,
type: ROCK,
image: //image path
}
case PAPER:return{
...state,
type: PAPER,
image: //image path
}
case SCISSORS:return{
...state,
type: SCISSORS,
image: //image path
}
default: return state
}
}

4. The Store: This holds the state of the application

export const store=createStore(GameReducers)

5. The Provider: The Provider component is wrapped around the root component of the React application. It receives the Redux store as a prop and makes it available to all descendant components.


<Provider store={store}>
<App/>
</Provider>

To see how Redux was applied to a game of rock-paper-scissors check out

or to view the live website click the link below:

In conclusion, developing strong and scalable applications becomes possible with an understanding of Redux state management in React.js. Redux offers a predictable state container that makes managing data across the application easier, particularly in situations when state needs to be changed or shared between many components.

Developers may efficiently organize and maintain the state logic of their applications by comprehending Redux’s essential elements, which include the state, actions, reducers, store, and provider. Redux is an invaluable tool for creating sophisticated applications quickly because of its capacity to centralize state management and provide smooth integration with React through React-Redux

Redux provides a solid solution for reliably and effectively keeping application state, whether it’s handling complex game logic like rock-paper-scissors or managing the state of large-scale systems.

Redux gives developers the ability to design dynamic and responsive user interfaces while keeping a clean and organized codebase, enabling them to take on the issues of state management head-on. So go ahead and explore Redux’s features to take your React.js applications to the next level of scalability and performance.

--

--