0% completed
In this chapter, you will be learning about one of the most popular tools used in React applications to manage states called Redux. By the end of this chapter, you'll be knowledgeable abut what Redux is, when and how to use it and how it compares to other state management approaches.
Redux is a third-party library used to manage global state. Global state, as we learned earlier in the course, is state that is accessible to multiple components. Redux is a standalone library but easily integrates with React apps using react-redux
library.
State management is a vital process in React applications because it is all about how data is handled, updated and shared across a web application. In React applications, state holds information about the current environment, such as UI state, form values, and user inputs. Without proper state management, an app can become difficult to maintain, as data may be scattered across components.
With redux, complex state management is handled better and makes the application scalable.
To have a solid understanding of Redux, you need to have a good grasp of
useReducer
. If you're not familiar with it, please go back and refresh your memory on howuseReducer
works.
Redux provides a centralized and predictable way to manage state across an application. It solves several problems, including:
Context API: React's built-in state management solution. It’s great for passing down state to deeply nested components without prop drilling. However, it may cause performance issues with large applications as it triggers re-renders of all consuming components.
useState: A React hook used for local component state. It’s simple and useful for small apps, but for complex apps with a lot of shared state, it becomes hard to maintain.
useReducer: A React hook often used for more complex state logic, such as handling multiple state transitions. It’s great for managing local component states but lacks centralized state management for large apps.
Redux is built on some foundational principles such as:
Single source of truth: The entire application state is stored in a single JavaScript object, called the store. This centralizes the state and makes it predictable.
State is read-only: The state can only be read; it cannot be modified directly. Instead, actions are dispatched to update the state.
Changes are made with pure functions: State changes are handled by reducers, which are pure functions that take the current state and an action to return a new state.
Redux consists of three main parts:
Next, we dive into using Redux practically in our React project and application.
.....
.....
.....