Skip to content
Research notes on data, code, and analysis +1 2354253223 [email protected] Los Angeles, California

A Practical Guide to Redux

A team discussing a shared screen at a meeting table

What is Redux?

Redux is a state management library that lets you connect directly to application state from anywhere in your app. It also allows you to manipulate application state from anywhere in your app. But, to work its magic, Redux requires that your app have a single data store. This post focuses on using Redux in a React app.

This post has three parts: boilerplate, elements of Redux (the bulk of this post), and a sample app (an adorable, or rather super tough, Most Wanted list app).

Clone the repository and check out the demo. If you want to type out the code yourself, start by initializing a new project with the Create React App tool. When it is done, move into the folder and install Redux, the React Redux bindings, and Axios. Axios has nothing to do with Redux, but it is the library used for AJAX calls in the demo app.

What problem does Redux solve?

In a React app, data is fetched in a parent component and then passed down to child components through props. Things get complicated when there are many layers of components and data, and state and the functions that modify this state are passed through numerous components to get from origin to destination. This path can be difficult to remember and it leaves many places for errors to be introduced.

With Redux, any component can be connected directly to state. This is not to say that data is no longer passed down from parent components to child components via props. Rather, this path can now be direct, with no passing props down from parent to great great great great grandchild.

It is not good practice to have all components connect to application state. It is best to have parent or container components connect to state and pass state directly to children.

Terminology: components connected to state are usually called container or smart components, and they usually execute logic too. Components that do not have state of their own, and receive state from container components, are called dumb components. Sometimes these dumb components are functional components. Both container components and functional components are implemented in the demo app.

The action creator and the thunk

Technically an action creator only needs to fire off Redux's dispatch function along with an object. That is not quite what is going on here. In the code below, the main function dispatches another function. This second function is called a thunk, and it returns the object or action.

const addPersonAsync = (person) => (dispatch) => {
  dispatch({ type: "ADD_PERSON", person });
};

Terminology: in the context of Redux Thunk, a thunk is a second function that performs delayed logic by being asynchronously returned by a first function. That is the Thunk middleware at work. This double function strategy allows us to wait for an asynchronous operation, such as fetching data, to complete, and then the action is returned by the thunk.

The adjusted order, including reducers, is: dispatch, action creator, thunk, action, reducer. In this case the double function strategy is not necessary, but it is a general pattern to follow so that if an operation is asynchronous, you are already set up to deal with it.

Here is a case in which an async operation is actually taking place. Below is the code for fetching the data for the demo app's Most Wanted list.

const fetchPeople = () => (dispatch) => {
  axios.get("/api/people").then((response) => {
    dispatch({ type: "LOAD_PEOPLE", people: response.data });
  });
};

Both patterns keep the data flow readable. A reader can follow the path from a click, to a request, to the store, and back to the view without holding the whole component tree in their head.

References and further reading

Primary documentation for the libraries used in this note.

  1. React for the component model.
  2. Create React App for project scaffolding.
  3. React Redux for the official bindings.
  4. Redux Thunk for async action creators.
  5. Redux style guide for recommended patterns.
  6. React Redux connect for wiring components to state.
  7. Axios for HTTP requests.
  8. Axios on npm for installation.
  9. Create React App documentation for scaffolding.
  10. MDN Web Docs for the request model behind AJAX.
  11. MDN Fetch API for the modern request alternative.