The ones familiar with react will know that react is a component-based front end library tool that connects the various segments of the web page. In react, we use props (short for properties) in a component which allows the usage of non-static variables. With the help of props, we can pass these variables into various other components (child components) from the parent component. If you want to experiment with the code we’ve covered in this article, you can access it in the GitHub repository provided. Feel free to use it as a starting point for your own applications or as a reference as you continue to learn and explore the world of Redux. The Provider component takes in the store as a prop and passes it down to all the child components that need access to it.

Once we have imported these necessary components, we can proceed to write code for Task.jsx. This can be incredibly useful when debugging your application, as you can inspect the state and actions in real-time. Redux is a state management library that allows you to manage the state of your JavaScript applications more efficiently and predictably. Note that, inside the reducer, we’re also returning state at the end.

Using middleware

This is because React only allows for a uni-directional flow of data. That means data cannot be sent from a child to a parent; it has to flow downward from the parent to the child. This thought model works very well with Redux where we cannot directly modify the state. Instead, we dispatch actions that intend to change the state, and then separately, we observe the resulting state changes. Wrap your root component with the Provider component from react-redux to make the Redux store available to all components in your application. Redux makes testing much easier because it uses functional JavaScript as a base, and small independent functions are easy to test.

what is redux for

Also, the store is the single source of truth for your application’s state. This means that any component in your application can access it to retrieve and update data. Imagine you are building a house and need to keep track of all the materials you use and how what is redux and why it matters much money you spend. Instead of keeping track of everything in your head or on a piece of paper, you could use a ledger to keep track of every transaction. Redux works similarly by keeping track of your application’s state in a single place called the “store.”

Get set up with LogRocket’s modern error tracking in minutes:

This allows you to debug your applications effectively, including using powerful techniques like “time-travel debugging”. One major benefit of Redux is the ability to navigate through the state’s history, allowing developers to observe how the state has changed throughout the app’s lifecycle. However, it is important to implement Redux only if it fits your requirements and your project needs a state management tool.

  • The combineReducers() utility makes the file structure much easier to maintain.
  • It lets your React components read data from a Redux store, and dispatch actions to the store to update state.
  • These functions don’t rely on the state they’re called from, and they return only one and the same result for any provided argument.
  • There can either be one reducer if it is a simple app or multiple reducers taking care of different parts or slices of the global state in a bigger application.

The index.js file represents the root reducer, which combines all the individual reducers in the application. In contrast, the taskReducer.js file is one of the individual reducers that will be combined in the root reducer. Now that you understand the basics of Redux and how it works, let’s create a simple real-world project. For this example, we’ll create a basic ToDo List application where you can add and delete tasks. In Redux, dispatch is a function provided by the store that allows you to send an action to update the state of your application. When you call dispatch, the store runs an action through all of the available reducers, which in turn update the state accordingly.

Step 8: How to dispatch actions

To create a slice to manage our ToDo application, create a new file named src/features/todo/todoSlice.js and add the following code. To create the actions, create a new folder called “actions” in the src directory and then create a new file called index.js. This file will contain all of the action creators for our application. As I mentioned before, actions represent something that happened in the application. For example, when a user adds a new task, it triggers an “add task” action. Similarly, when a user deletes a task, it triggers a “delete task” action.

Note that we update the state immutably by copying the existing state and updating the
copy, instead of modifying the original object directly. Take some time to think about the kind of app you’re building, and decide what tools would be best to help solve the problems you’re working on. Redux helps you deal with shared state management, but like any tool, it has tradeoffs. It also adds some indirection to your code, and asks you to follow certain restrictions.

It is the Official Redux UI Bindings for React​

That counter example was small, but it does show all the working pieces of a real Redux app. Everything we’ll talk about in the following sections expands on those basic pieces. A “store” is a container that holds your application’s global state. The rest of the description on this page focuses solely on the Redux core library (the redux package). We’ll talk about the other Redux-related packages as we go through the rest of the tutorial.

what is redux for

In the code above, we created a component consisting of an input field and a button. When a user clicks on the “Add task” button, the addNewTask function is executed. In the case of addTodo, the type property is set to “ADD_TASK”, indicating that a new task has been added. The payload property contains an object with the new task’s id and text values. The id is generated using the new Date().getTime() method creates a unique identifier based on the current timestamp. The switch statement inside the reducer handles different cases based on the “type” of the action.

How to set up Redux Store

Finally, import the components into your App.jsx file and render them. The above command sequence will create a new React project using the Vite build tool and install all necessary dependencies. In this scenario, the chef represents the action creator, who follows the recipe to create the dish, similar to how an action creator creates an action based on predefined properties. The Redux store is like a giant container that holds all the data for your application. In addition to the type, we can also pass extra information as a part of the action.

what is redux for

If you’re just getting started with Redux, this video is a great resource for beginners. While it’s mostly used as a state management tool with React, you can use Redux with any other JavaScript framework or library. It’s lightweight at 2KB (including dependencies), so you don’t have to worry about it making your application’s asset size bigger. Redux is an example of a JavaScript library whose enduring popularity is a testament to its value. In this guide, we’ll provide a foundational understanding of Redux, highlighting its functionalities and why you should use it. We’ll explore its benefits by using a simple but practical component.

Where Can Redux Be Used?

It lets your React components read data from a Redux store, and dispatch actions to the store to update state. We’ll take the cart component which displays the number of items in a user’s cart. The state of the cart component will consist of all the items the user has added to the cart and the total number of those items. At all times the application is up and running, this component has to show the updated number of items in the user’s cart. Now imagine what happens when a state has to be shared between components that are far apart in the component tree.