Skip to content

Commit 65ce197

Browse files
committed
Update README.md
1 parent 25946cf commit 65ce197

File tree

1 file changed

+59
-85
lines changed

1 file changed

+59
-85
lines changed

README.md

Lines changed: 59 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
react-redux
22
=========================
33

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.
56

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`.**
108
119
## Table of Contents
1210

11+
- [React Native](#react-native)
1312
- [Quick Start](#quick-start)
1413
- [API](#api)
1514
- [`connect([mapState], [mapDispatch], [mergeProps])`](#connectmapstate-mapdispatch-mergeprops)
1615
- [`<Provider store>`](#provider-store)
1716
- [Recipes](#recipes)
1817
- [License](#license)
1918

19+
## React Native
20+
21+
What you get from `react-redux` is for React.
22+
For React Native, import from `react-redux/native` instead.
23+
2024
## Quick Start
2125

2226
React bindings for Redux embrace the idea of [dividing “smart” and “dumb” components](https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0).
@@ -75,10 +79,11 @@ export default class Counter extends Component {
7579

7680
Here’s how we hook it up to the Redux Store.
7781

78-
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.
7985

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!
8287

8388
```js
8489
import { Component } from 'react';
@@ -87,89 +92,40 @@ import { connect } from 'react-redux';
8792
// Assuming this is our “dumb” counter
8893
import Counter from '../components/Counter';
8994

90-
// Assuming these are Redux action creators
91-
import { increment } from './actionCreators';
95+
// Assuming action creators as named exports:
96+
import * as counterActionCreators from '../actionsCreators';
9297

98+
// Which part of the Redux global state does our component want to receive as props?
9399
function mapState(state) {
94-
// Which part of the Redux global state does our component want to receive as props?
95100
return {
96101
counter: state.counter
97102
};
98103
}
99104

100-
class CounterContainer extends Component {
101-
render() {
102-
// connect() call below will inject `dispatch` and
103-
// every key returned by `mapState` as props into our container:
104-
const { dispatch, counter } = this.props;
105-
106-
// render our “dumb” component, hooking up state to data props
107-
// and using “dispatch action produced by this action creator” as callbacks.
108-
// this is a “bridge” between a Redux-aware world above and Redux-unaware world below.
109-
110-
return (
111-
<Counter counter={counter}
112-
increment={() => dispatch(increment())} />
113-
);
114-
}
115-
}
116-
117105
// Don't forget to actually use connect!
118-
export default connect(mapState)(CounterContainer);
119-
120-
// You might have noticed that we used parens twice.
121-
// This is called partial applications, and it lets people
122-
// use ES7 decorator proposal syntax:
123-
//
124-
// @connect(mapState)
125-
// export default class CounterContainer { ... }
126-
//
127-
// Don’t forget decorators are experimental! And they
128-
// desugar to function calls anyway as example above demonstrates.
106+
export default connect(mapState, counterActionCreators)(CounterContainer);
129107
```
130108

131-
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
132110

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.
138112

139-
import { Component } from 'react';
140-
import { connect } from 'react-redux';
113+
### Support for Decorators
141114

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)
145-
import * as CounterActionCreators from './actionCreators';
146-
import Counter from '../components/Counter';
115+
You might have noticed that we used parens twice. This is called partial applications, and it lets people
116+
use ES7 decorator proposal syntax:
147117

148-
function mapState(state) {
149-
return {
150-
counter: state.counter
151-
};
152-
}
153-
154-
class CounterContainer extends Component {
155-
render() {
156-
const { dispatch, counter } = this.props;
157-
158-
// This time, we use `bindActionCreators` to bind many action creators
159-
// to a particular dispatch function from our Redux store.
118+
```js
119+
// Unstable syntax! It might change or break in production.
120+
@connect(mapState)
121+
export default class CounterContainer { ... }
122+
```
160123

161-
return (
162-
<Counter counter={counter}
163-
{...bindActionCreators(CounterActionCreators, dispatch)} />
164-
);
165-
}
166-
}
124+
Don’t forget decorators are experimental! And they desugar to function calls anyway as example above demonstrates.
167125

168-
// Don't forget to actually use connect!
169-
export default connect(mapState)(CounterContainer);
170-
```
126+
### Additional Flexibility
171127

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.
173129

174130
### Injecting Redux store
175131

@@ -248,34 +204,42 @@ present in order to use the `connect` component.
248204

249205
## Recipes
250206

207+
##### Inject just `dispatch` and don't listen to store
208+
251209
```js
252-
// Inject just `dispatch` and don't listen to store
253210
export default connect()(TodoApp);
211+
```
254212

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
257215
export default connect(state => state)(TodoApp);
216+
```
258217

218+
##### Inject `dispatch` and `todos`
259219

260-
// Inject `dispatch` and `todos`
220+
```js
261221
function mapState(state) {
262222
return { todos: state.todos };
263223
}
264224

265225
export default connect(mapState)(TodoApp);
226+
```
266227

228+
##### Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...)
267229

268-
// Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...)
230+
```js
269231
import * as actionCreators from './actionCreators';
270232

271233
function mapState(state) {
272234
return { todos: state.todos };
273235
}
274236

275237
export default connect(mapState, actionCreators)(TodoApp);
238+
```
276239

240+
##### Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...) as `actions`
277241

278-
// Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...) as `actions`
242+
```js
279243
import * as actionCreators from './actionCreators';
280244
import { bindActionCreators } from 'redux';
281245

@@ -288,9 +252,11 @@ function mapDispatch(dispatch) {
288252
}
289253

290254
export default connect(mapState, actionCreators)(TodoApp);
255+
```
291256

257+
##### Inject `todos` and a specific action creator (`addTodo`)
292258

293-
// Inject `todos` and a specific action creator (`addTodo`)
259+
```js
294260
import { addTodo } from './actionCreators';
295261
import { bindActionCreators } from 'redux';
296262

@@ -303,9 +269,11 @@ function mapDispatch(dispatch) {
303269
}
304270

305271
export default connect(mapState, mapDispatch)(TodoApp);
272+
```
306273

274+
##### Inject `todos`, todoActionCreators as `todoActions`, and counterActionCreators as `counterActions`
307275

308-
// Inject `todos`, todoActionCreators as `todoActions`, and counterActionCreators as `counterActions`
276+
```js
309277
import * as todoActionCreators from './todoActionCreators';
310278
import * as counterActionCreators from './counterActionCreators';
311279
import { bindActionCreators } from 'redux';
@@ -322,9 +290,11 @@ function mapDispatch(dispatch) {
322290
}
323291

324292
export default connect(mapState, mapDispatch)(TodoApp);
293+
```
325294

295+
##### Inject `todos`, and todoActionCreators and counterActionCreators together as `actions`
326296

327-
// Inject `todos`, and todoActionCreators and counterActionCreators together as `actions`
297+
```js
328298
import * as todoActionCreators from './todoActionCreators';
329299
import * as counterActionCreators from './counterActionCreators';
330300
import { bindActionCreators } from 'redux';
@@ -340,9 +310,11 @@ function mapDispatch(dispatch) {
340310
}
341311

342312
export default connect(mapState, mapDispatch)(TodoApp);
313+
```
343314

315+
##### Inject `todos`, and all todoActionCreators and counterActionCreators directly as props
344316

345-
// Inject `todos`, and all todoActionCreators and counterActionCreators directly as props
317+
```js
346318
import * as todoActionCreators from './todoActionCreators';
347319
import * as counterActionCreators from './counterActionCreators';
348320
import { bindActionCreators } from 'redux';
@@ -356,9 +328,11 @@ function mapDispatch(dispatch) {
356328
}
357329

358330
export default connect(mapState, mapDispatch)(TodoApp);
331+
```
359332

333+
##### Inject `todos` of a specific user depending on props, and inject `props.userId` into the action
360334

361-
// Inject `todos` of a specific user depending on props, and inject `props.userId` into the action
335+
```js
362336
import * as actionCreators from './actionCreators';
363337

364338
function mapState(state) {

0 commit comments

Comments
 (0)