A React application can share multiple points of data across components. In many cases, managing the data shared can become a complex task.
Redux is a library for managing and updating application states. It provides a centralized “store” for the state that is shared across your entire application, with rules ensuring that the state can only be updated in a predictable fashion using events called “actions”.
Redux works well with applications that have a large amount of global state that is accessed by many of the application’s components. The goal of Redux is to provide scaleable and predictable state management.
In Redux, a store is a container that manages the global state of your application.
As the single source of truth, the store
is the center of every Redux application. It has the ability to update the global state and subscribes elements of an application’s UI to changes in the state. Rather than accessing the state directly, Redux provides functions through the store to interact with the state.
In Redux, an action is a plain JavaScript object that represents an intention to change the store’s state. Action objects must have a type
property with a user-defined string value that describes the action being taken.
Optional properties can be added to the action object. One common property added is conventionally called payload
, which is used to supply data necessary to perform the desired action.
/*Basic action object for a shopping listthat removes all items from the list*/const clearItems = {type: 'shopping/clear'}/*Action object for a shopping listthat adds an item to the list*/const addItem = {type: 'shopping/addItem',payload: 'Chocolate Cake'}
In Redux, a reducer, also known as a reducing function, is a JavaScript function that takes the current state
of the store and an action
as parameters.
Reducers calculate the new state
based on the action it receives. Reducers are the only way the store’s current state
can be changed within a Redux application. They are an important part of Redux’s one-way data flow model.
/*A reducer function that handles 2 actionsor returns the current state as a default*/const shoppingReducer = (state = [],action) => {switch (action.type) {case "shopping/clear":return [];case "shopping/addItem":return [...state,action.payload];default:/*If the reducer doesn't careabout this action type, returnthe existing state unchanged*/return state;}}
Immutable updates involve creating a new copy of a mutable value, like an array or object, instead of modifying the original. This ensures data integrity and avoids unexpected behaviors.
Some strategies for ensuring immutable updates are:
...
) to create copies of objects and arraysstate = ['sleep', 'work', 'relax']const newState = {...state, 'read'}/*newState = ['sleep', 'work', 'relax', 'read']*/state = {location: 'North Pole',temperatures: [-10, -14, -9, -18]}const newState = {...state,temperatures: [...state.temperatures,-22, -17]}/*newState = {location: 'North Pole',temperatures: [-10, -14, -9, -18, -22, -17]}*/
Reducers must follow these three rules:
These rules help support Redux’s scaleable and predictable state management. Some common behaviors to avoid inside reducers are network requests, generating random numbers, and using asynchronous functions.
Redux follows a one-way data flow model:
Store → View → Actions → Store