Modern React with Redux

Master React and Redux with React Router, Webpack, and Create-React-App.

Getting Started with Redux

Redux is a predictable state container for JavaScript apps.

It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debuggerarrow-up-right.

You can use Redux together with Reactarrow-up-right, or with any other view library. It is tiny (2kB, including dependencies), but has a large ecosystem of addons available.

Installationarrow-up-right

Redux Toolkitarrow-up-right

Redux Toolkitarrow-up-right is our official recommended approach for writing Redux logic. It wraps around the Redux core, and contains packages and functions 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.

RTK includes utilities that help simplify many common use cases, including store setuparrow-up-right, creating reducers and writing immutable update logicarrow-up-right, and even creating entire "slices" of state at oncearrow-up-right.

Whether you're a brand new Redux user setting up your first project, or an experienced user who wants to simplify an existing application, Redux Toolkitarrow-up-right can help you make your Redux code better.

Redux Toolkit is available as a package on NPM for use with a module bundler or in a Node application:

# NPMnpm install @reduxjs/toolkit# Yarnyarn add @reduxjs/toolkit

Copy

Create a React Redux Apparrow-up-right

The recommended way to start new apps with React and Redux is by using the official Redux+JS templatearrow-up-right or Redux+TS templatearrow-up-right for Create React Apparrow-up-right, which takes advantage of Redux Toolkitarrow-up-right and React Redux's integration with React components.

# Redux + Plain JS templatenpx create-react-app my-app --template redux# Redux + TypeScript templatenpx create-react-app my-app --template redux-typescript

Copy

The Redux core library is available as a package on NPM for use with a module bundler or in a Node application:

Copy

It is also available as a precompiled UMD package that defines a window.Redux global variable. The UMD package can be used as a <script> tagarrow-up-right directly.

For more details, see the Installationarrow-up-right page.

Basic Examplearrow-up-right

The whole global state of your app is stored in an object tree inside a single store. The only way to change the state tree is to create an action, an object describing what happened, and dispatch it to the store. To specify how state gets updated in response to an action, you write pure reducer functions that calculate a new state based on the old state and the action.

Copy

Instead of mutating the state directly, you specify the mutations you want to happen with plain objects called actions. Then you write a special function called a reducer to decide how every action transforms the entire application's state.

In a typical Redux app, there is just a single store with a single root reducing function. As your app grows, you split the root reducer into smaller reducers independently operating on the different parts of the state tree. This is exactly like how there is just one root component in a React app, but it is composed out of many small components.

This architecture might seem like a lot for a counter app, but the beauty of this pattern is how well it scales to large and complex apps. It also enables very powerful developer tools, because it is possible to trace every mutation to the action that caused it. You can record user sessions and reproduce them just by replaying every action.

Redux Toolkit Examplearrow-up-right

Redux Toolkit simplifies the process of writing Redux logic and setting up the store. With Redux Toolkit, that same logic looks like:

Copy

Redux Toolkit allows us to write shorter logic that's easier to read, while still following the same Redux behavior and data flow.

We have a variety of resources available to help you learn Redux.

Redux Essentials Tutorialarrow-up-right

The Redux Essentials tutorialarrow-up-right is a "top-down" tutorial that teaches "how to use Redux the right way", using our latest recommended APIs and best practices. We recommend starting there.

Redux Fundamentals Tutorialarrow-up-right

The Redux Fundamentals tutorialarrow-up-right is a "bottom-up" tutorial that teaches "how Redux works" from first principles and without any abstractions, and why standard Redux usage patterns exist.

Learn Modern Redux Livestreamarrow-up-right

Redux maintainer Mark Erikson appeared on the "Learn with Jason" show to explain how we recommend using Redux today. The show includes a live-coded example app that shows how to use Redux Toolkit and React-Redux hooks with Typescript, as well as the new RTK Query data fetching APIs.

See the "Learn Modern Redux" show notes pagearrow-up-right for a transcript and links to the example app source.

Additional Tutorialsarrow-up-right

Other Resourcesarrow-up-right

Help and Discussionarrow-up-right

The #redux channelarrow-up-right of the Reactiflux Discord communityarrow-up-right is our official resource for all questions related to learning and using Redux. Reactiflux is a great place to hang out, ask questions, and learn - come join us!

You can also ask questions on Stack Overflowarrow-up-right using the #redux tagarrow-up-right.

If you have a bug report or need to leave other feedback, please file an issue on the Github repoarrow-up-right

Should You Use Redux?arrow-up-right

Redux is a valuable tool for organizing your state, but you should also consider whether it's appropriate for your situation. Don't use Redux just because someone said you should - take some time to understand the potential benefits and tradeoffs of using it.

Here are some suggestions on when it makes sense to use Redux:

  • You have reasonable amounts of data changing over time

  • You need a single source of truth for your state

  • You find that keeping all your state in a top-level component is no longer sufficient

For more thoughts on how Redux is meant to be used, see:

Last updated