Skip to content

[Docs Rewrite] Create the new "Redux Fundamentals" tutorial #3894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 53 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
cc26cd5
Add Fundamentals Overview page
markerikson Sep 28, 2020
1f273dd
Fill out Part 1
markerikson Sep 28, 2020
48d9f58
Add Part 2
markerikson Sep 28, 2020
ebcb826
Stub out parts 3, 8, and 9
markerikson Sep 28, 2020
e8e6eef
Add Part 3 skeleton
markerikson Sep 28, 2020
3e6efd5
Add "Designing State" section
markerikson Sep 28, 2020
1f93ca4
Assorted cleanup and file renames
markerikson Sep 28, 2020
4175521
Clarify code sample usage
markerikson Sep 28, 2020
ee3bdde
Fill out reducers section
markerikson Sep 29, 2020
eee29ff
Add Part 4 page
markerikson Sep 29, 2020
adeff0f
Polish content
markerikson Sep 29, 2020
d5c5fa3
Clarify randomness
markerikson Sep 29, 2020
963034d
Fix broken link
markerikson Sep 29, 2020
36441df
Rename "S,R,A" to "S,A,R" to match ordering
markerikson Sep 30, 2020
be83b93
Fill out part 4
markerikson Sep 30, 2020
e5afdf9
Redo tutorial page descriptions
markerikson Oct 1, 2020
f5e1521
Add most of part 5
markerikson Oct 1, 2020
3c7105f
Write the rest of part 5
markerikson Oct 1, 2020
3f2915e
Fix link
markerikson Oct 1, 2020
87b4e1e
Fix copy-pasted header
markerikson Oct 1, 2020
266f5ce
Add "state and forms" section
markerikson Oct 2, 2020
564fea0
Update parts 3 and 4 with code tweaks and screenshots
markerikson Oct 3, 2020
b3c2d32
Update snippets based on example app and add sandboxes
markerikson Oct 4, 2020
a5d3573
Replace "TODO" comments with "FIXME" for visibility
markerikson Oct 9, 2020
ccd14f7
First half of Part 6 "Async Logic"
markerikson Oct 9, 2020
3921125
Fix title
markerikson Oct 9, 2020
cd3c210
Add async data flow diagram
markerikson Oct 15, 2020
e28db60
Add loading/saving explanations
markerikson Oct 18, 2020
65f3f9d
Add "What You've Learned" for Async Logic
markerikson Oct 18, 2020
c0ac9d3
Fix header examples and add CodeSandbox for part 6
markerikson Oct 18, 2020
3b12e80
Add action creators section
markerikson Oct 21, 2020
1005b75
Add selectors section
markerikson Oct 21, 2020
ee82774
Add "what you've learned"
markerikson Oct 21, 2020
6ce5f0f
Add Tao of Redux links
markerikson Oct 21, 2020
c19930b
Add remaining "standard patterns" content
markerikson Oct 25, 2020
c7c89fb
Misc cleanup
markerikson Oct 25, 2020
92d9ecc
Clarify normalization logic
markerikson Oct 25, 2020
bb61dc5
Add first section of "Modern Redux"
markerikson Oct 28, 2020
0e45be9
Write the rest of "Modern Redux"
markerikson Oct 29, 2020
3e16402
General cleanup
markerikson Oct 30, 2020
d956e1a
Update "Part N" links to include page titles
markerikson Oct 31, 2020
11f8dec
Reword intro section and add additional info
markerikson Oct 31, 2020
67e9ca4
Change comment color to increase contrast
markerikson Oct 31, 2020
fb1556b
Clean up README and "Getting Started"
markerikson Oct 31, 2020
7842337
Replace links to Basics/Advanced pages
markerikson Oct 31, 2020
6c7b295
Update redirects to point to corresponding "Fundamentals" pages
markerikson Oct 31, 2020
c9e4f09
Remove Basics/Advanced from sidebar
markerikson Oct 31, 2020
9ed7d61
Remove legacy Basics/Advanced pages
markerikson Oct 31, 2020
08f62e5
Update README/Getting Started examples and add links
markerikson Oct 31, 2020
4b8d794
Merge branch 'master' into docs/fundamentals-tutorial
markerikson Oct 31, 2020
adac8bb
Fix blockquote/detailed explanation colors in dark mode
markerikson Oct 31, 2020
bd858da
Review edits: more mutation info, "Summary" sections
markerikson Nov 2, 2020
84b80e6
Fix typo
markerikson Nov 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 91 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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).
It is tiny (2kB, including dependencies), and has a rich ecosystem of addons.

[![build status](https://img.shields.io/travis/reduxjs/redux/master.svg?style=flat-square)](https://travis-ci.org/reduxjs/redux)
[![npm version](https://img.shields.io/npm/v/redux.svg?style=flat-square)](https://www.npmjs.com/package/redux)
Expand All @@ -16,11 +16,19 @@ It is tiny (2kB, including dependencies).

## Installation

[**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.

```
npm install @reduxjs/toolkit react-redux
```

For more details, please see [the Installation docs page](https://redux.js.org/introduction/installation).
For the Redux core library by itself:

```
npm install redux
```

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

## Documentation

Expand All @@ -39,12 +47,15 @@ For Offline docs, please see: [devdocs](http://devdocs.io/redux/)

### Redux Essentials Tutorial

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 strongly recommend you to start from there.
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

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.

### Additional Tutorials

- The Redux docs [**Basic tutorial**](https://redux.js.org/basics/basic-tutorial) and [**Advanced tutorial**](https://redux.js.org/advanced/advanced-tutorial) are "bottom-up" tutorials that teaches how Redux works by starting from its main principles.
- The Redux repository contains several example projects demonstrating various aspects of how to use Redux. Almost all the examples have a corresponding CodeSandbox sandbox. This is an interactive version of the code that you can play with online. Please see the complete list of examples in the **[Examples page](https://redux.js.org/introduction/examples)**.
- 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://egghead.io/series/getting-started-with-redux)** 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](http://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/)
Expand Down Expand Up @@ -74,6 +85,7 @@ Yes, these guidelines are subjective and vague, but this is for a good reason. T

> **For more thoughts on how Redux is meant to be used, please see:**<br>
>
> - **[When (and when not) to reach for Redux](https://changelog.com/posts/when-and-when-not-to-reach-for-redux)**
> - **[You Might Not Need Redux](https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367)**<br>
> - **[The Tao of Redux, Part 1 - Implementation and Intent](http://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-1/)**<br>
> - **[The Tao of Redux, Part 2 - Practice and Philosophy](http://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-2/)**
Expand All @@ -85,38 +97,36 @@ Dan Abramov (author of Redux) wrote Redux while working on his React Europe talk

## Influences

Redux evolves the ideas from [Flux](http://facebook.github.io/flux/), but avoids its complexity by taking cues from [Elm](https://github.com/evancz/elm-architecture-tutorial/).
Even if you haven't used Flux or Elm, Redux only takes a few minutes to get start with.

## The Gist
Redux evolves the ideas of [Flux](http://facebook.github.io/flux/), but avoids its complexity by taking cues from [Elm](https://github.com/evancz/elm-architecture-tutorial/).
Even if you haven't used Flux or Elm, Redux only takes a few minutes to get started with.

The whole state of your app is stored in an object tree inside a single _store_.
The only way to change the state tree is to emit an _action_, that is an object describing what happened.
To specify how the actions transform the state tree, you should write pure _reducers_.
## Basic Example

That's it!
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.

```js
import { createStore } from 'redux'

/**
* This is a reducer, a pure function with (state, action) => state signature.
* It describes how an action transforms the state into the next state.
* 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 shape of the state is up to you: it can be a primitive, an array, an object,
* or even an Immutable.js data structure. The only important part is that you should
* 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.
*
* In this example, we use a `switch` statement and strings, but you can use a helper that
* follows a different convention (such as function maps) if it makes sense for your
* project.
* 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 counter(state = 0, action) {
function counterReducer(state = { value: 0 }, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
case 'counter/incremented':
return { value: state.value + 1 }
case 'counter/decremented':
return { value: state.value - 1 }
default:
return state
}
Expand All @@ -128,23 +138,71 @@ let store = createStore(counter)

// 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.
// However it can also be handy to persist the current state in the localStorage.
// 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: 'INCREMENT' })
// 1
store.dispatch({ type: 'INCREMENT' })
// 2
store.dispatch({ type: 'DECREMENT' })
// 1
store.dispatch({ type: 'counter/incremented' })
// {value: 1}
store.dispatch({ type: 'counter/incremented' })
// {value: 2}
store.dispatch({ type: 'counter/decremented' })
// {value: 1}
```

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.

This architecture might seem like an overkill 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.
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

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

```js
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.actions

const store = configureStore({
reducer: counterSlice.reducer
})

// Can still subscribe to the store
store.subscribe(() => console.log(store.getState()))

// Still pass action objects to `dispatch`, but they're created for us
store.dispatch(incremented())
// {value: 1}
store.dispatch(incremented())
// {value: 2}
store.dispatch(decremented())
// {value: 1}
```

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

## Examples

Expand All @@ -154,16 +212,13 @@ Almost all examples have a corresponding CodeSandbox sandbox. This is an interac
- [**Counter**](https://redux.js.org/introduction/examples#counter): [Source](https://github.com/reduxjs/redux/tree/master/examples/counter) | [Sandbox](https://codesandbox.io/s/github/reduxjs/redux/tree/master/examples/counter)
- [**Todos**](https://redux.js.org/introduction/examples#todos): [Source](https://github.com/reduxjs/redux/tree/master/examples/todos) | [Sandbox](https://codesandbox.io/s/github/reduxjs/redux/tree/master/examples/todos)
- [**Todos with Undo**](https://redux.js.org/introduction/examples#todos-with-undo): [Source](https://github.com/reduxjs/redux/tree/master/examples/todos-with-undo) | [Sandbox](https://codesandbox.io/s/github/reduxjs/redux/tree/master/examples/todos-with-undo)
- [**Todos w/ Flow**](https://redux.js.org/introduction/examples#todos-flow): [Source](https://github.com/reduxjs/redux/tree/master/examples/todos-flow)
- [**TodoMVC**](https://redux.js.org/introduction/examples#todomvc): [Source](https://github.com/reduxjs/redux/tree/master/examples/todomvc) | [Sandbox](https://codesandbox.io/s/github/reduxjs/redux/tree/master/examples/todomvc)
- [**Shopping Cart**](https://redux.js.org/introduction/examples#shopping-cart): [Source](https://github.com/reduxjs/redux/tree/master/examples/shopping-cart) | [Sandbox](https://codesandbox.io/s/github/reduxjs/redux/tree/master/examples/shopping-cart)
- [**Tree View**](https://redux.js.org/introduction/examples#tree-view): [Source](https://github.com/reduxjs/redux/tree/master/examples/tree-view) | [Sandbox](https://codesandbox.io/s/github/reduxjs/redux/tree/master/examples/tree-view)
- [**Async**](https://redux.js.org/introduction/examples#async): [Source](https://github.com/reduxjs/redux/tree/master/examples/async) | [Sandbox](https://codesandbox.io/s/github/reduxjs/redux/tree/master/examples/async)
- [**Universal**](https://redux.js.org/introduction/examples#universal): [Source](https://github.com/reduxjs/redux/tree/master/examples/universal)
- [**Real World**](https://redux.js.org/introduction/examples#real-world): [Source](https://github.com/reduxjs/redux/tree/master/examples/real-world) | [Sandbox](https://codesandbox.io/s/github/reduxjs/redux/tree/master/examples/real-world)

If you're new to the NPM ecosystem and have troubles getting a project up and running, or aren't sure where to paste the gist above, check out [simplest-redux-example](https://github.com/jackielii/simplest-redux-example) that uses Redux together with React and Browserify.

## Testimonials

> [“Love what you're doing with Redux”](https://twitter.com/jingc/status/616608251463909376)
Expand Down
Loading