Using Redux
In this section, we will learn about Redux before using it to refactor the app we have been working on to use it.
Understanding Redux
Redux is a mature state management library that was first released in 2015. It was released before React context and became a popular approach for shared state management.
Creating a store
In Redux, the state lives in a centralized immutable object referred to as a store. There is only a single store for the whole app. Like useReducer
, the state in a store is updated by dispatching an action, which is an object containing the type of change and any data required to make the change. An action is handled by a reducer
function, which creates a new version of the state.
In the past, a lot of code was needed to set up a Redux store and consume it in a React component. Today, a companion library called Redux Toolkit reduces the code required to use Redux. A Redux store can be created using the Redux Toolkit’s configureStore...