State Management: Redux

If you've been working with React for a while, you may have heard of Redux. Redux is a state management library that makes it easier to manage the state of a complex application.

In this post, we'll walk through the basics of setting up a Redux application and creating actions and reducers to manage the state.

Prerequisites

Before we get started, it's important to have a basic understanding of JavaScript and React. If you're new to these technologies, we recommend checking out some introductory tutorials before diving into Redux.

Setting up a Redux application

The first step in using Redux is to set up a Redux store. The store is the single source of truth for the state of the application. To create a store, we use the createStore function from the Redux library:

Terminal
import { createStore } from 'redux';

const store = createStore(reducer);

The reducer argument is a function that takes the current state and an action as arguments and returns a new state. We'll dive deeper into reducers in the next section.

Actions and Reducers

In Redux, actions and reducers are two fundamental concepts that are used to manage the state of an application.

Actions

Actions are plain JavaScript objects that represent some sort of event or change in the state of the application. They have a 'type' property that describes the type of the action and a 'payload' property that contains any data that needs to be passed along with the action.

Here's an example of an action that represents adding an item to a shopping cart:

Example
{
  type: 'ADD_TO_CART',
  payload: {
    id: 1,
    name: 'Product 1',
    price: 9.99
  }
}

In this example, the 'type'' property is set to 'ADD_TO_CART', which tells the reducer that this action represents adding an item to the cart. The payload property contains the details of the item that is being added to the cart.

Reducers

Reducers are functions that take the current state and an action as arguments, and return a new state. The purpose of a reducer is to calculate the new state of the application based on the current state and the action.

Here's an example of a reducer that handles the 'ADD_TO_CART' action:

Example
const cartReducer = (state = [], action) => {
  switch (action.type) {
    case 'ADD_TO_CART':
      return [...state, action.payload];
    default:
      return state;
  }
};

In this example, the cartReducer function takes the current state (which is an array of items in the shopping cart) and the 'ADD_TO_CART' action as arguments. The switch statement checks the type property of the action to determine what type of action it is, and returns a new state based on the action. In this case, if the action is an 'ADD_TO_CART' action, the reducer creates a new array that contains all the items in the current state plus the new item passed in the payload property of the action.

Actions and reducers work together to manage the state of a Redux application. Actions represent changes in the state, and reducers calculate the new state of the application based on those actions. By using actions and reducers, we can manage complex application states in a predictable and easy-to-understand way.

Conclusion

That's a basic overview of how to get started with Redux. We've covered how to set up a store, create actions and reducers, and connect components to the store. We encourage you to continue learning about Redux by exploring the official documentation and trying out some of the more advanced features like middleware and asynchronous actions. Good luck!

Published: Apr 12, 2023
Join my newsletter to stay updated about the latest I'm working on and share resources I've come across.