Skip to content

Commit a7bdd45

Browse files
authored
[Docs Rewrite] Create the new "Redux Fundamentals" tutorial (#3894)
2 parents b948ccc + 84b80e6 commit a7bdd45

File tree

70 files changed

+5402
-3528
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+5402
-3528
lines changed

README.md

Lines changed: 91 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Redux is a predictable state container for JavaScript apps.
66
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).
77

88
You can use Redux together with [React](https://reactjs.org), or with any other view library.
9-
It is tiny (2kB, including dependencies).
9+
It is tiny (2kB, including dependencies), and has a rich ecosystem of addons.
1010

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

1717
## Installation
1818

19+
[**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.
20+
1921
```
2022
npm install @reduxjs/toolkit react-redux
2123
```
2224

23-
For more details, please see [the Installation docs page](https://redux.js.org/introduction/installation).
25+
For the Redux core library by itself:
26+
27+
```
28+
npm install redux
29+
```
30+
31+
For more details, see [the Installation docs page](https://redux.js.org/introduction/installation).
2432

2533
## Documentation
2634

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

4048
### Redux Essentials Tutorial
4149

42-
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.
50+
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.
51+
52+
### Redux Fundamentals Tutorial
53+
54+
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.
4355

4456
### Additional Tutorials
4557

46-
- 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.
47-
- 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)**.
58+
- 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)**.
4859
- 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
4960
- 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/)
5061
- Dave Ceddia's post [**A Complete React Redux Tutorial for Beginners**](https://daveceddia.com/redux-tutorial/)
@@ -74,6 +85,7 @@ Yes, these guidelines are subjective and vague, but this is for a good reason. T
7485

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

8698
## Influences
8799

88-
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/).
89-
Even if you haven't used Flux or Elm, Redux only takes a few minutes to get start with.
90-
91-
## The Gist
100+
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/).
101+
Even if you haven't used Flux or Elm, Redux only takes a few minutes to get started with.
92102

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

97-
That's it!
105+
The whole global state of your app is stored in an object tree inside a single _store_.
106+
The only way to change the state tree is to create an _action_, an object describing what happened, and _dispatch_ it to the store.
107+
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.
98108

99109
```js
100110
import { createStore } from 'redux'
101111

102112
/**
103-
* This is a reducer, a pure function with (state, action) => state signature.
104-
* It describes how an action transforms the state into the next state.
113+
* This is a reducer - a function that takes a current state value and an
114+
* action object describing "what happened", and returns a new state value.
115+
* A reducer's function signature is: (state, action) => newState
105116
*
106-
* The shape of the state is up to you: it can be a primitive, an array, an object,
107-
* or even an Immutable.js data structure. The only important part is that you should
117+
* The Redux state should contain only plain JS objects, arrays, and primitives.
118+
* The root state value is usually an object. It's important that you should
108119
* not mutate the state object, but return a new object if the state changes.
109120
*
110-
* In this example, we use a `switch` statement and strings, but you can use a helper that
111-
* follows a different convention (such as function maps) if it makes sense for your
112-
* project.
121+
* You can use any conditional logic you want in a reducer. In this example,
122+
* we use a switch statement, but it's not required.
113123
*/
114-
function counter(state = 0, action) {
124+
function counterReducer(state = { value: 0 }, action) {
115125
switch (action.type) {
116-
case 'INCREMENT':
117-
return state + 1
118-
case 'DECREMENT':
119-
return state - 1
126+
case 'counter/incremented':
127+
return { value: state.value + 1 }
128+
case 'counter/decremented':
129+
return { value: state.value - 1 }
120130
default:
121131
return state
122132
}
@@ -128,23 +138,71 @@ let store = createStore(counter)
128138

129139
// You can use subscribe() to update the UI in response to state changes.
130140
// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
131-
// However it can also be handy to persist the current state in the localStorage.
141+
// There may be additional use cases where it's helpful to subscribe as well.
132142

133143
store.subscribe(() => console.log(store.getState()))
134144

135145
// The only way to mutate the internal state is to dispatch an action.
136146
// The actions can be serialized, logged or stored and later replayed.
137-
store.dispatch({ type: 'INCREMENT' })
138-
// 1
139-
store.dispatch({ type: 'INCREMENT' })
140-
// 2
141-
store.dispatch({ type: 'DECREMENT' })
142-
// 1
147+
store.dispatch({ type: 'counter/incremented' })
148+
// {value: 1}
149+
store.dispatch({ type: 'counter/incremented' })
150+
// {value: 2}
151+
store.dispatch({ type: 'counter/decremented' })
152+
// {value: 1}
143153
```
144154

145155
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.
146156

147-
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.
157+
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.
158+
159+
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.
160+
161+
### Redux Toolkit Example
162+
163+
Redux Toolkit simplifies the process of writing Redux logic and setting up the store. With Redux Toolkit, that same logic looks like:
164+
165+
```js
166+
import { createSlice, configureStore } from '@reduxjs/toolkit'
167+
168+
const counterSlice = createSlice({
169+
name: 'counter',
170+
initialState: {
171+
value: 0
172+
},
173+
reducers: {
174+
incremented: state => {
175+
// Redux Toolkit allows us to write "mutating" logic in reducers. It
176+
// doesn't actually mutate the state because it uses the Immer library,
177+
// which detects changes to a "draft state" and produces a brand new
178+
// immutable state based off those changes
179+
state.value += 1
180+
},
181+
decremented: state => {
182+
state.value -= 1
183+
}
184+
}
185+
})
186+
187+
export const { incremented, decremented } = counterSlice.actions
188+
189+
const store = configureStore({
190+
reducer: counterSlice.reducer
191+
})
192+
193+
// Can still subscribe to the store
194+
store.subscribe(() => console.log(store.getState()))
195+
196+
// Still pass action objects to `dispatch`, but they're created for us
197+
store.dispatch(incremented())
198+
// {value: 1}
199+
store.dispatch(incremented())
200+
// {value: 2}
201+
store.dispatch(decremented())
202+
// {value: 1}
203+
```
204+
205+
Redux Toolkit allows us to write shorter logic that's easier to read, while still following the same Redux behavior and data flow.
148206

149207
## Examples
150208

@@ -154,16 +212,13 @@ Almost all examples have a corresponding CodeSandbox sandbox. This is an interac
154212
- [**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)
155213
- [**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)
156214
- [**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)
157-
- [**Todos w/ Flow**](https://redux.js.org/introduction/examples#todos-flow): [Source](https://github.com/reduxjs/redux/tree/master/examples/todos-flow)
158215
- [**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)
159216
- [**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)
160217
- [**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)
161218
- [**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)
162219
- [**Universal**](https://redux.js.org/introduction/examples#universal): [Source](https://github.com/reduxjs/redux/tree/master/examples/universal)
163220
- [**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)
164221

165-
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.
166-
167222
## Testimonials
168223

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

0 commit comments

Comments
 (0)