You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+91-36Lines changed: 91 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Redux is a predictable state container for JavaScript apps.
6
6
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).
7
7
8
8
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.
@@ -16,11 +16,19 @@ It is tiny (2kB, including dependencies).
16
16
17
17
## Installation
18
18
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
+
19
21
```
20
22
npm install @reduxjs/toolkit react-redux
21
23
```
22
24
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).
24
32
25
33
## Documentation
26
34
@@ -39,12 +47,15 @@ For Offline docs, please see: [devdocs](http://devdocs.io/redux/)
39
47
40
48
### Redux Essentials Tutorial
41
49
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.
43
55
44
56
### Additional Tutorials
45
57
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)**.
48
59
- 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
49
60
- 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/)
50
61
- 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
74
85
75
86
> **For more thoughts on how Redux is meant to be used, please see:**<br>
76
87
>
88
+
> -**[When (and when not) to reach for Redux](https://changelog.com/posts/when-and-when-not-to-reach-for-redux)**
77
89
> -**[You Might Not Need Redux](https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367)**<br>
78
90
> -**[The Tao of Redux, Part 1 - Implementation and Intent](http://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-1/)**<br>
79
91
> -**[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
85
97
86
98
## Influences
87
99
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.
92
102
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
96
104
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.
98
108
99
109
```js
100
110
import { createStore } from'redux'
101
111
102
112
/**
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
105
116
*
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
108
119
* not mutate the state object, but return a new object if the state changes.
109
120
*
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.
// The only way to mutate the internal state is to dispatch an action.
136
146
// 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}
143
153
```
144
154
145
155
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.
146
156
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:
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
-
167
222
## Testimonials
168
223
169
224
> [“Love what you're doing with Redux”](https://twitter.com/jingc/status/616608251463909376)
0 commit comments