Search
Close this search box.

How to Use Redux and Redux Toolkit Tutorial for Beginners

Redux Toolkit is the recommended way to build production apps with Redux, and is built on all of the concepts that we will look at throughout this tutorial. Once you understand the core concepts covered here, you’ll understand how to use Redux Toolkit more efficiently. It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test.

what is redux for

The createStore function returns a store that we can use to manage the application data. We may very well maintain the internal state of the components inside them, but as and when an application grows bigger, it may have to share some state between components. This is not just only to show them in the view, but also to manage or update them or perform some logic based on their value. Once you understand how everything fits together, we’ll look at using Redux Toolkit to simplify things.

Example Project – Real App Implementation

Finally, we export the store object so that it can be imported and used in other parts of the application. Once you have installed the needed dependencies, create a new “slice” using the createSlice function. A slice is a portion of the Redux store that is responsible for managing a specific piece of state. With that, you now have a fully functional ToDo application powered by Redux! The source code for the app is available on this GitHub repository.

what is redux for

It creates a tangible data structure to represent the state of your app that you can read from and write to. That way, you can see otherwise invisible states while you’re working with them. This is because React only allows for a uni-directional flow of data.

Redux middleware

As the number of reducers and actions increase, it can become challenging to manage the different pieces and keep track of everything. 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. 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.

what is redux for

The process of subscribing to the store, checking for updated data, and triggering a re-render can be made more generic and reusable. A UI binding library like React Redux handles the store interaction what is redux and why it matters logic, so you don’t have to write that code yourself. It’s been used by many companies (Uber, Khan Academy, Twitter) and in many projects (Apollo, WordPress’ Calypso), successfully in production.

Redux Toolkit

For this app, we’re going to track a single number with the current value of our counter. You can use Redux together with React, or with any other view library. It is tiny (2kB, including dependencies), but has a large ecosystem of addons available. Explore how Superglue and Hotwire revolutionize frontend development with HTML over the wire, enhancing performance, flexibility, and ease of use. With Redux, you can persist some of the app’s state to localStorage and restore it after a refresh. As we mentioned earlier, Redux is a standalone library that can be used with different JavaScript frameworks including Angular, Inferno, Vue, Preact, React, etc.

what is redux for

The export statement exports the generated action creators, which can be used in other parts of your app to dispatch actions to the slice. Now that we have created the necessary actions, we can move on to creating the components that will dispatch these actions. First, we import the Provider function and the Redux store we created into our main.jsx. Then, we wrap our App component with the Provider function and pass the store as a prop.

Key Redux Toolkit features to know

Imagine you are building a house and need to keep track of all the materials you use and how 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.” Most of the time, you don’t want the whole state object from a slice. This is why you need selectors, which are simple functions that accept a Redux state as an argument and return data that is derived from that state.

  • In this comprehensive guide, we’ll explore what Redux is, why it’s valuable, and how to use it effectively in your React applications with practical examples.
  • Now, the recommended method is using configureStore, which will not only create a store for you but will also accept reducer functions.
  • Then, we create a store object by calling configureStore and passing it an object with a reducer property.
  • This is because the reducer gets called immediately once we pass it to the createStore function.

The createSlice function automatically generates action creators and action types based on the names of the reducer functions you provide. So you don’t have to define the action creators yourself manually. Your Redux store holds the single source of truth for your application state. Now, the recommended method is using configureStore, which will not only create a store for you but will also accept reducer functions. Redux is an open-source JavaScript library designed for managing the state of a web application. It follows the principles of a predictable state container, which means that all application state is stored in a single, immutable object called the store.

Understanding Redux: A tutorial with examples

It also provides us with some important APIs using which we can make changes to the existing state as well as fetch the current state of the application. 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.

In the above example, on clicking the button, we had dispatched an action with an action creator called addItemToCart(). This action creator has dispatched an action with the type ADD_ITEM_TO_CART. In the following section, we will dive deep into the core concepts of Redux – the store, actions and reducers. Whenever a user adds an item to the cart, the application has to internally handle that action by adding that item to the cart object. It has to maintain its state internally and also show the user the total number of items in the cart in the UI. We pass the reducer function to createStore, which uses the reducer function
to generate the initial state, and to calculate any future updates.

You may need to keep track of the items in a user’s cart, their payment information, and their shipping details. In this article, I will explain Redux in the simplest possible way. As someone who initially struggled with understanding Redux, I know how frustrating it can be to learn a new concept. But I hope this article will help make the concepts of Redux more accessible to beginner learners.

what is redux for

Sign up for our Newsletter

We are a 360° Highly effecient digital Marketing Agency helping Brands to build loyalty, enhance engagements and drive conversions.

Take a Step Ahead.
Say Hi.

info@roundigital.in

Visit Us

5th Floor, Unit NO: 534-535, Block B2, Spaze ITech
Park, Sohna Rd, Sector 49, Gurugram, Haryana 122018