Skip to content
Entre em contato comigo e descubra tudo o que o mundo do artesanato tem à oferecer! 21 99382-9259 | kcestudiocriativo@gmail.com

Understanding Redux: A tutorial with examples LogRocket Blog

If you’re unsure about whether you need it, you probably don’t. This usually happens when your app grows to a scale where managing app state becomes a hassle and you start looking for ways to make it simplify it. So before you dive into the world of advanced state management (e.g. Redux), consider using the tools React ships with out of the box. You don’t need Redux if your application’s state is easy to manage without it. If you’re building a house, you probably don’t need a jackhammer even if you’ve learned how to use it. Consider an application with two functionalities “Users” and “Products”.

why redux

By convention, we put that information in a field called payload. We’ll look at where and how this is important a bit later, as well as some easier ways to write immutable update logic. By defining and separating the concepts involved in state management and enforcing rules that maintain independence between views and states, we give our code more structure and maintainability. Here, we’ll dispatch the actions that will make the reducer add 1 or
subtract 1 from the current counter value. Now that we have a reducer function, we can create a store instance by
calling the Redux library createStore API.

Redux Fundamentals, Part 1: Redux Overview

In other words, if you don’t require data from multiple sources, there’s no need to introduce Redux. You won’t run into data inconsistency problems when accessing a single data source per view. As the official binding library for React and Redux, React Redux has a large community of users. This makes it easier to ask for help, learn about best practices, use libraries that build on top of React Redux, and reuse your knowledge across different applications. If performance is a concern, the best way to improve performance is to skip unnecessary re-renders, so that components only re-render when their data has actually changed.

why redux

Rather a reducer produces a new instance of the state with all the necessary updates. We’ll talk more about actions and reducers in the following sections. Being a state management library, Redux will basically store and manage all the application’s states. While these were the patterns originally shown in the Redux docs, they unfortunately require a lot of very verbose and repetitive code. On top of that, the boilerplate-y code lead to more opportunities to make mistakes.

Understanding Redux: A tutorial with examples

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’s a trade-off between short term and long term productivity. First, we define the initial state for the application by setting the count equal to zero.

why redux

Context API provides a different approach to tackling the data flow problem between React’s deeply nested components. Context has been around with React for quite a while, but it has changed significantly since its inception. Up to version 16.3, it was a way to handle the state data outside the React component tree. It was an experimental feature not recommended for most use cases. Imagine a pyramid structure of seven components where each component as two child components.

Core Principles of Redux

They are the only way you can send data from your application to your Redux store. The data can be from user interactions, API calls, or even form submissions. There is a central store that holds the entire state of the application.

We get a state management system when using it with useContext and useReducer hooks. Redux itself is a standalone library that can be used with any UI layer or framework, including React, Angular, Vue, Ember, and vanilla JS. Although Redux and React are commonly used together, they are independent of each other. This helps you restrict any part of the view or any network calls to write/update the state directly.

Why Redux Toolkit is How To Use Redux Today

State management is essentially a way to facilitate the communication and sharing of data across components. It creates a tangible data structure to represent the state of your app that you can read from and write what is redux to. That way, you can see otherwise invisible states while you’re working with them. While it’s mostly used as a state management tool with React, you can use Redux with any other JavaScript framework or library.

This also eliminates the need to write more code, which is awesome in my opinion. When working with functional programming, it’s important to understand concepts like pure functions, anonymous functions, closures and higher order functions just to mention a few. That’s where Redux saves the day for you; it eases the complexities that spring up in such applications. If you’ve got some experience in React, you’ll know that React’s data flow is such that parent components pass down props to child components. Whenever an action is dispatched, all the reducers are activated. Each reducer filters out the action using a switch statement switching on the action type.

Redux Toolkit (also known as “RTK” for short) is our official recommended approach for writing Redux logic. The @reduxjs/toolkit package wraps around the core redux package, and contains API methods and common dependencies that we think are essential for building a Redux app. Redux Toolkit builds in our suggested best practices, simplifies most Redux tasks, prevents common mistakes, and makes it easier to write Redux applications. None of this code specifically depends on any API from the redux core library. It was also common, though not strictly required, to spread the code for one feature across multiple files like actions/todos.js, constants/todos.js, and reducers/todos.js.

  • You won’t run into data inconsistency problems when accessing a single data source per view.
  • The type should be a readable name so that
    anyone who looks at this code understands what it means.
  • Separating out the state management into a reducer breaks up the code and makes it more readable.
  • Redux guides you towards writing code that is predictable and testable, which helps give you confidence that your application will work as expected.

Although it’s a reasonably efficient pattern that promotes pure functions, it might be an overhead for simple applications that involve only a couple of UI changes. On top of that, don’t forget that Redux is an in-memory state store. In other words, if your application crashes, you lose your entire application state. This means that you have to use a caching solution to create a backup of your application state, which again creates extra overhead. Note how in the above example, we dispatch an action on click of the button. Or rather, to be more specific, we dispatch something known as an action creator – that is, the function addItemToCart().

What is Redux used for?

It’s not an easy task to manage each component’s state while sharing it with many other components. You’ll likely experience data inconsistency bugs, a fearsome nightmare for frontend developers. React is generally fast, but by default any updates to a component will cause React to re-render all of the components inside that part of the component tree. This does require work, and if the data for a given component hasn’t changed, then re-rendering is likely some wasted effort because the requested UI output would be the same. 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 logic, so you don’t have to write that code yourself.

why redux

Back To Top