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
+59-85Lines changed: 59 additions & 85 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,26 @@
1
1
react-redux
2
2
=========================
3
3
4
-
Higher-order React components for [Redux](https://github.com/gaearon/redux).
4
+
Official React bindings for [Redux](https://github.com/gaearon/redux).
5
+
Performant and flexible.
5
6
6
-
What you get from `react-redux` is for React.
7
-
For React Native, import from `react-redux/native` instead.
8
-
9
-
>**Note: There is a project called “redux-react” on NPM that is completely unrelated to the official bindings. This documentation (and any other official Redux documentation) is for `react-redux`.**
7
+
>**Note: There is a project called `redux-react` on NPM that is [completely unrelated](https://github.com/cgarvis/redux-react/issues/1) to the official bindings. This documentation (and any other official Redux documentation) is for `react-redux`.**
For React Native, import from `react-redux/native` instead.
23
+
20
24
## Quick Start
21
25
22
26
React bindings for Redux embrace the idea of [dividing “smart” and “dumb” components](https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0).
We will use `connect()` function provided by `react-redux` to turn a “dumb” `Counter` into a smart component. With the current API, we’ll need to add an intermediate `CounterContainer` component, but we will soon make `connect` API more powerful so this won’t be required. The `connect()` function lets you specify *which exactly* state from the Redux store your component wants to track. This lets you subscribe on any level of granularity.
82
+
We will use `connect()` function provided by `react-redux` to turn a “dumb” `Counter` into a smart component. The `connect()` function lets you specify *which exactly* state from the Redux store your component wants to track. This lets you subscribe on any level of granularity.
83
+
84
+
Passing action creator functions as the second parameter will bind them to the specific store instance, and they will be injected as props with the same names they were exported with.
79
85
80
-
Our `CounterContainer` that’s necessary to hook `Counter` up to a Redux store looks like this:
81
-
(This will be much less verbose in the next versions.)
86
+
Why don’t we bind action creators to a store right away? This is because of the so-called “universal” apps that need to render on the server. They would have a different store instance for every request, so we don’t know the store instance during the definition!
82
87
83
88
```js
84
89
import { Component } from'react';
@@ -87,89 +92,40 @@ import { connect } from 'react-redux';
As you can see, action creators in Redux just return actions, but we need to manually “bind” them to the `dispatch` function for our Redux store. Why don’t we bind action creators to a store right away? This is because of the so-called “universal” apps that need to render on the server. They would have a different store instance for every request, so we don’t know the store instance during the definition!
109
+
### Usage Notes
132
110
133
-
### Binding many action creators
134
-
135
-
Binding can get cumbersome, so Redux provides a `bindActionCreators` helper to turn many action creator methods into an object with methods called the same, but bound to a particular `dispatch` function:
136
-
137
-
```js
111
+
You can have many `connect()`-ed components in your app at any depth, and you can even nest them. It is however preferable that you try to only `connect()` top-level components such as route handlers, so the data flow in your application stays predictable.
138
112
139
-
import { Component } from'react';
140
-
import { connect } from'react-redux';
113
+
### Support for Decorators
141
114
142
-
// A helper provided by Redux!
143
-
import { bindActionCreators } from'redux';
144
-
// Import many action creators as a single object (like `require('./actionCreators')` in CommonJS)
Don’t forget decorators are experimental! And they desugar to function calls anyway as example above demonstrates.
167
125
168
-
// Don't forget to actually use connect!
169
-
exportdefaultconnect(mapState)(CounterContainer);
170
-
```
126
+
### Additional Flexibility
171
127
172
-
You can have many `connect()`-ed components in your app at any depth, and you can even nest them. It is however preferable that you try to only `connect()` top-level components such as route handlers, so the data flow in your application stays predictable.
128
+
This the most basic usage, but `connect()` supports many other different patterns: just passing the vanilla `dispatch()` function down, binding multiple action creators, putting them as `actions` prop, selecting parts of state and binding action creators depending on `props`, and so on. Check out [Recipes](#recipes) for some ideas about advanced `connect()` usage.
173
129
174
130
### Injecting Redux store
175
131
@@ -248,34 +204,42 @@ present in order to use the `connect` component.
248
204
249
205
## Recipes
250
206
207
+
##### Inject just `dispatch` and don't listen to store
208
+
251
209
```js
252
-
// Inject just `dispatch` and don't listen to store
253
210
exportdefaultconnect()(TodoApp);
211
+
```
254
212
255
-
256
-
// Inject `dispatch` and every field in the global state (SLOW!)
213
+
##### Inject `dispatch` and every field in the global state (SLOW!)
214
+
```js
257
215
exportdefaultconnect(state=> state)(TodoApp);
216
+
```
258
217
218
+
##### Inject `dispatch` and `todos`
259
219
260
-
// Inject `dispatch` and `todos`
220
+
```js
261
221
functionmapState(state) {
262
222
return { todos:state.todos };
263
223
}
264
224
265
225
exportdefaultconnect(mapState)(TodoApp);
226
+
```
266
227
228
+
##### Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...)
267
229
268
-
// Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...)
0 commit comments