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

# Modern React with Redux

## 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 debugger](https://github.com/reduxjs/redux-devtools).

You can use Redux together with [React](https://reactjs.org), or with any other view library. It is tiny (2kB, including dependencies), but has a large ecosystem of addons available.

### Installation[​](https://redux.js.org/introduction/getting-started#installation) <a href="#installation" id="installation"></a>

#### Redux Toolkit[​](https://redux.js.org/introduction/getting-started#redux-toolkit) <a href="#redux-toolkit" id="redux-toolkit"></a>

[**Redux Toolkit**](https://redux-toolkit.js.org) 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 setup](https://redux-toolkit.js.org/api/configureStore), [creating reducers and writing immutable update logic](https://redux-toolkit.js.org/api/createreducer), and even [creating entire "slices" of state at once](https://redux-toolkit.js.org/api/createslice).

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 Toolkit**](https://redux-toolkit.js.org) 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 App[​](https://redux.js.org/introduction/getting-started#create-a-react-redux-app) <a href="#create-a-react-redux-app" id="create-a-react-redux-app"></a>

The recommended way to start new apps with React and Redux is by using the [official Redux+JS template](https://github.com/reduxjs/cra-template-redux) or [Redux+TS template](https://github.com/reduxjs/cra-template-redux-typescript) for [Create React App](https://github.com/facebook/create-react-app), which takes advantage of [**Redux Toolkit**](https://redux-toolkit.js.org) 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

#### Redux Core[​](https://redux.js.org/introduction/getting-started#redux-core) <a href="#redux-core" id="redux-core"></a>

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

```
# NPMnpm install redux# Yarnyarn add redux
```

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>` tag](https://unpkg.com/redux/dist/redux.js) directly.

For more details, see the [Installation](https://redux.js.org/introduction/installation) page.

### Basic Example[​](https://redux.js.org/introduction/getting-started#basic-example) <a href="#basic-example" id="basic-example"></a>

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.

```
import { createStore } from 'redux'/** * This is a reducer - a function that takes a current state value and an * action object describing "what happened", and returns a new state value. * A reducer's function signature is: (state, action) => newState * * The Redux state should contain only plain JS objects, arrays, and primitives. * The root state value is usually an object.  It's important that you should * not mutate the state object, but return a new object if the state changes. * * You can use any conditional logic you want in a reducer. In this example, * we use a switch statement, but it's not required. */function counterReducer(state = { value: 0 }, action) {  switch (action.type) {    case 'counter/incremented':      return { value: state.value + 1 }    case 'counter/decremented':      return { value: state.value - 1 }    default:      return state  }}// Create a Redux store holding the state of your app.// Its API is { subscribe, dispatch, getState }.let store = createStore(counterReducer)// You can use subscribe() to update the UI in response to state changes.// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.// There may be additional use cases where it's helpful to subscribe as well.store.subscribe(() => console.log(store.getState()))// The only way to mutate the internal state is to dispatch an action.// The actions can be serialized, logged or stored and later replayed.store.dispatch({ type: 'counter/incremented' })// {value: 1}store.dispatch({ type: 'counter/incremented' })// {value: 2}store.dispatch({ type: 'counter/decremented' })// {value: 1}
```

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 Example[​](https://redux.js.org/introduction/getting-started#redux-toolkit-example) <a href="#redux-toolkit-example" id="redux-toolkit-example"></a>

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

```
import { createSlice, configureStore } from '@reduxjs/toolkit'const counterSlice = createSlice({  name: 'counter',  initialState: {    value: 0  },  reducers: {    incremented: state => {      // Redux Toolkit allows us to write "mutating" logic in reducers. It      // doesn't actually mutate the state because it uses the Immer library,      // which detects changes to a "draft state" and produces a brand new      // immutable state based off those changes      state.value += 1    },    decremented: state => {      state.value -= 1    }  }})export const { incremented, decremented } = counterSlice.actionsconst store = configureStore({  reducer: counterSlice.reducer})// Can still subscribe to the storestore.subscribe(() => console.log(store.getState()))// Still pass action objects to `dispatch`, but they're created for usstore.dispatch(incremented())// {value: 1}store.dispatch(incremented())// {value: 2}store.dispatch(decremented())// {value: 1}
```

Copy

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

### Learn Redux[​](https://redux.js.org/introduction/getting-started#learn-redux) <a href="#learn-redux" id="learn-redux"></a>

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

#### Redux Essentials Tutorial[​](https://redux.js.org/introduction/getting-started#redux-essentials-tutorial) <a href="#redux-essentials-tutorial" id="redux-essentials-tutorial"></a>

The [**Redux Essentials tutorial**](https://redux.js.org/tutorials/essentials/part-1-overview-concepts) 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 Tutorial[​](https://redux.js.org/introduction/getting-started#redux-fundamentals-tutorial) <a href="#redux-fundamentals-tutorial" id="redux-fundamentals-tutorial"></a>

The [**Redux Fundamentals tutorial**](https://redux.js.org/tutorials/fundamentals/part-1-overview) 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 Livestream[​](https://redux.js.org/introduction/getting-started#learn-modern-redux-livestream) <a href="#learn-modern-redux-livestream" id="learn-modern-redux-livestream"></a>

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 page](https://www.learnwithjason.dev/let-s-learn-modern-redux) for a transcript and links to the example app source.

#### Additional Tutorials[​](https://redux.js.org/introduction/getting-started#additional-tutorials) <a href="#additional-tutorials" id="additional-tutorials"></a>

* The Redux repository contains several example projects demonstrating various aspects of how to use Redux. Almost all examples have a corresponding CodeSandbox sandbox. This is an interactive version of the code that you can play with online. See the complete list of examples in the [**Examples page**](https://redux.js.org/introduction/examples).
* Redux creator Dan Abramov's \*\*free \*\*[**"Getting Started with Redux" video series**](https://app.egghead.io/playlists/fundamentals-of-redux-course-from-dan-abramov-bd5cc867) and [**Building React Applications with Idiomatic Redux**](https://egghead.io/courses/building-react-applications-with-idiomatic-redux) video courses on Egghead.io
* Redux maintainer Mark Erikson's [**"Redux Fundamentals" conference talk**](https://blog.isquaredsoftware.com/2018/03/presentation-reactathon-redux-fundamentals/) and [**"Redux Fundamentals" workshop slides**](https://blog.isquaredsoftware.com/2018/06/redux-fundamentals-workshop-slides/)
* Dave Ceddia's post [**A Complete React Redux Tutorial for Beginners**](https://daveceddia.com/redux-tutorial/)

#### Other Resources[​](https://redux.js.org/introduction/getting-started#other-resources) <a href="#other-resources" id="other-resources"></a>

* The [**Redux FAQ**](https://redux.js.org/faq) answers many common questions about how to use Redux, and the [**"Using Redux" docs section**](https://redux.js.org/usage/index) has information on handling derived data, testing, structuring reducer logic, and reducing boilerplate.
* Redux maintainer Mark Erikson's [**"Practical Redux" tutorial series**](https://blog.isquaredsoftware.com/series/practical-redux/) demonstrates real-world intermediate and advanced techniques for working with React and Redux (also available as [**an interactive course on Educative.io**](https://www.educative.io/collection/5687753853370368/5707702298738688)).
* The [**React/Redux links list**](https://github.com/markerikson/react-redux-links) has categorized articles on working with [reducers and selectors](https://github.com/markerikson/react-redux-links/blob/master/redux-reducers-selectors.md), [managing side effects](https://github.com/markerikson/react-redux-links/blob/master/redux-side-effects.md), [Redux architecture and best practices](https://github.com/markerikson/react-redux-links/blob/master/redux-architecture.md), and more.
* Our community has created thousands of Redux-related libraries, addons, and tools. The [**"Ecosystem" docs page**](https://redux.js.org/introduction/ecosystem) lists our recommendations, and there's a complete listing available in the [**Redux addons catalog**](https://github.com/markerikson/redux-ecosystem-links).

### Help and Discussion[​](https://redux.js.org/introduction/getting-started#help-and-discussion) <a href="#help-and-discussion" id="help-and-discussion"></a>

The [**#redux channel**](https://discord.gg/0ZcbPKXt5bZ6au5t) of the [**Reactiflux Discord community**](https://www.reactiflux.com) 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 Overflow](https://stackoverflow.com) using the [**#redux tag**](https://stackoverflow.com/questions/tagged/redux).

If you have a bug report or need to leave other feedback, [please file an issue on the Github repo](https://github.com/reduxjs/redux)

### Should You Use Redux?[​](https://redux.js.org/introduction/getting-started#should-you-use-redux) <a href="#should-you-use-redux" id="should-you-use-redux"></a>

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:**
>
> * [**Redux FAQ: When should I use Redux?**](https://redux.js.org/faq/general#when-should-i-use-redux)
> * [**You Might Not Need Redux**](https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367)
> * [**The Tao of Redux, Part 1 - Implementation and Intent**](https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-1/)
> * [**The Tao of Redux, Part 2 - Practice and Philosophy**](https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-2/)
> * [**Redux FAQ**](https://redux.js.org/faq)
