Skip to content

Commit 423a590

Browse files
committed
Update README.md
1 parent 3079d00 commit 423a590

File tree

1 file changed

+61
-28
lines changed

1 file changed

+61
-28
lines changed

README.md

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ For React Native, import from `react-redux/native` instead.
1414
- [API](#api)
1515
- [`connect([mapState], [mapDispatch], [mergeProps])`](#connect)
1616
- [`<Provider store>`](#provider)
17+
- [Recipes](#recipes)
1718
- [License](#license)
1819

1920
## Quick Start
@@ -218,130 +219,162 @@ React.render((
218219

219220
### `connect([mapState], [mapDispatch], [mergeProps])`
220221

222+
Returns a component class that injects the Redux Store’s `dispatch` as a prop into `Component` so it can dispatch Redux actions.
223+
224+
The returned component also subscribes to the updates of Redux store. Any time the state changes, it calls the `mapState` function passed to it. It is called a **selector**. The selector function takes a single argument of the entire Redux store’s state and returns an object to be passed as props. Use [reselect](https://github.com/faassen/reselect) to efficiently compose selectors and memoize derived data.
225+
226+
Both `dispatch` and every property returned by `mapState` will be provided to your `Component` as `props`.
227+
228+
It is the responsibility of a Smart Component to bind action creators to the given `dispatch` function and pass those
229+
bound creators to Dumb Components. Redux provides a `bindActionCreators` to streamline the process of binding action
230+
creators to the dispatch function.
231+
232+
**To use `connect()`, the root component of your app must be wrapped into `<Provider>{() => ... }</Provider>` before being rendered.**
233+
234+
See the usage example in the quick start above.
235+
236+
### `<Provider store>`
237+
238+
```js
239+
// Make store available to connect() below in hierarchy
240+
<Provider store={store}>
241+
{() => <MyRootComponent>}
242+
</Provider>
243+
```
244+
245+
The `Provider` component takes a `store` prop and a [function as a child](#child-must-be-a-function) with your root
246+
component. The `store` is then passed to the child via React's `context`. This is the entry point for Redux and must be
247+
present in order to use the `connect` component.
248+
249+
## Recipes
250+
221251
```js
222252
// Inject just `dispatch` and don't listen to store
223253
export default connect()(TodoApp);
224254

255+
225256
// Inject `dispatch` and every field in the global state (SLOW!)
226257
export default connect(state => state)(TodoApp);
227258

259+
228260
// Inject `dispatch` and `todos`
229261
function mapState(state) {
230262
return { todos: state.todos };
231263
}
264+
232265
export default connect(mapState)(TodoApp);
233266

267+
234268
// Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...)
235269
import * as actionCreators from './actionCreators';
270+
236271
function mapState(state) {
237272
return { todos: state.todos };
238273
}
274+
239275
export default connect(mapState, actionCreators)(TodoApp);
240276

277+
241278
// Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...) as `actions`
242279
import * as actionCreators from './actionCreators';
243280
import { bindActionCreators } from 'redux';
281+
244282
function mapState(state) {
245283
return { todos: state.todos };
246284
}
285+
247286
function mapDispatch(dispatch) {
248287
return { actions: bindActionCreators(actionCreators, dispatch) };
249288
}
289+
250290
export default connect(mapState, actionCreators)(TodoApp);
251291

292+
252293
// Inject `todos` and a specific action creator (`addTodo`)
253294
import { addTodo } from './actionCreators';
254295
import { bindActionCreators } from 'redux';
296+
255297
function mapState(state) {
256298
return { todos: state.todos };
257299
}
300+
258301
function mapDispatch(dispatch) {
259302
return { addTodo: bindActionCreators(addTodo, dispatch) };
260303
}
304+
261305
export default connect(mapState, mapDispatch)(TodoApp);
262306

307+
263308
// Inject `todos`, todoActionCreators as `todoActions`, and counterActionCreators as `counterActions`
264309
import * as todoActionCreators from './todoActionCreators';
265310
import * as counterActionCreators from './counterActionCreators';
266311
import { bindActionCreators } from 'redux';
312+
267313
function mapState(state) {
268314
return { todos: state.todos };
269315
}
316+
270317
function mapDispatch(dispatch) {
271318
return {
272319
todoActions: bindActionCreators(todoActionCreators, dispatch),
273320
counterActions: bindActionCreators(counterActionCreators, dispatch)
274321
};
275322
}
323+
276324
export default connect(mapState, mapDispatch)(TodoApp);
277325

326+
278327
// Inject `todos`, and todoActionCreators and counterActionCreators together as `actions`
279328
import * as todoActionCreators from './todoActionCreators';
280329
import * as counterActionCreators from './counterActionCreators';
281330
import { bindActionCreators } from 'redux';
331+
282332
function mapState(state) {
283333
return { todos: state.todos };
284334
}
335+
285336
function mapDispatch(dispatch) {
286337
return {
287338
actions: bindActionCreators({ ...todoActionCreators, ...counterActionCreators }, dispatch)
288339
};
289340
}
341+
290342
export default connect(mapState, mapDispatch)(TodoApp);
291343

344+
292345
// Inject `todos`, and all todoActionCreators and counterActionCreators directly as props
293346
import * as todoActionCreators from './todoActionCreators';
294347
import * as counterActionCreators from './counterActionCreators';
295348
import { bindActionCreators } from 'redux';
349+
296350
function mapState(state) {
297351
return { todos: state.todos };
298352
}
353+
299354
function mapDispatch(dispatch) {
300-
return bindActionCreators({ ...todoActionCreators, ...counterActionCreators }, dispatch);
355+
return bindActionCreators(Object.assign({}, todoActionCreators, counterActionCreators), dispatch);
301356
}
357+
302358
export default connect(mapState, mapDispatch)(TodoApp);
303359

360+
304361
// Inject `todos` of a specific user depending on props, and inject `props.userId` into the action
305362
import * as actionCreators from './actionCreators';
363+
306364
function mapState(state) {
307365
return { todos: state.todos };
308366
}
367+
309368
function mergeProps(selectedState, boundActions, props) {
310369
return Object.assign({}, props, {
311370
todos: selectedState.todos[props.userId],
312371
addTodo: (text) => boundActions.addTodo(props.userId, text)
313372
});
314373
}
315-
export default connect(mapState, actionCreators)(TodoApp);
316-
```
317-
318-
Returns a component class that injects the Redux Store’s `dispatch` as a prop into `Component` so it can dispatch Redux actions.
319-
320-
The returned component also subscribes to the updates of Redux store. Any time the state changes, it calls the `mapState` function passed to it. It is called a **selector**. The selector function takes a single argument of the entire Redux store’s state and returns an object to be passed as props. Use [reselect](https://github.com/faassen/reselect) to efficiently compose selectors and memoize derived data.
321-
322-
Both `dispatch` and every property returned by `mapState` will be provided to your `Component` as `props`.
323-
324-
It is the responsibility of a Smart Component to bind action creators to the given `dispatch` function and pass those
325-
bound creators to Dumb Components. Redux provides a `bindActionCreators` to streamline the process of binding action
326-
creators to the dispatch function.
327-
328-
**To use `connect()`, the root component of your app must be wrapped into `<Provider>{() => ... }</Provider>` before being rendered.**
329-
330-
See the usage example in the quick start above.
331-
332-
### `<Provider store>`
333374

334-
```js
335-
// Make store available to connect() below in hierarchy
336-
<Provider store={store}>
337-
{() => <MyRootComponent>}
338-
</Provider>
375+
export default connect(mapState, actionCreators)(TodoApp);
339376
```
340377

341-
The `Provider` component takes a `store` prop and a [function as a child](#child-must-be-a-function) with your root
342-
component. The `store` is then passed to the child via React's `context`. This is the entry point for Redux and must be
343-
present in order to use the `connect` component.
344-
345378
## License
346379

347380
MIT

0 commit comments

Comments
 (0)