Skip to content

Commit 3ad6f3b

Browse files
committed
Update README.md
1 parent c628e0e commit 3ad6f3b

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

README.md

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Performant and flexible.
1717
- [Quick Start](#quick-start)
1818
- [API](#api)
1919
- [`<Provider store>`](#provider-store)
20-
- [`connect([mapState], [mapDispatch], [mergeProps])(Component)`](#connectmapstate-mapdispatch-mergeprops)
20+
- [`connect([mapStateToProps], [mapDispatchToProps], [mergeProps])`](#connectmapstatetoprops-mapdispatchtoprops-mergeprops)
2121
- [License](#license)
2222

2323
## React Native
@@ -95,29 +95,29 @@ import Counter from '../components/Counter';
9595
import { increment } from '../actionsCreators';
9696

9797
// Which part of the Redux global state does our component want to receive as props?
98-
function mapState(state) {
98+
function mapStateToProps(state) {
9999
return {
100100
value: state.counter
101101
};
102102
}
103103

104104
// Which action creators does it want to receive by props?
105-
function mapDispatch(dispatch) {
105+
function mapDispatchToProps(dispatch) {
106106
return {
107107
onIncrement: () => dispatch(increment())
108108
};
109109
}
110110

111111
export default connect(
112-
mapState,
113-
mapDispatch
112+
mapStateToProps,
113+
mapDispatchToProps
114114
)(CounterContainer);
115115

116-
// You can also pass an object instead of defining `mapDispatch`:
117-
// export default connect(mapState, CounterActionCreators)(CounterContainer);
116+
// You can also pass an object instead of defining `mapDispatchToProps`:
117+
// export default connect(mapStateToProps, CounterActionCreators)(CounterContainer);
118118

119-
// Or you can pass `dispatch` down as a prop if you omit `mapDispatch`:
120-
// export default connect(mapState)(CounterContainer);
119+
// Or you can pass `dispatch` down as a prop if you omit `mapDispatchToProps`:
120+
// export default connect(mapStateToProps)(CounterContainer);
121121

122122
// See more recipes in detailed connect() examples below.
123123
```
@@ -136,7 +136,7 @@ use ES7 decorator proposal syntax:
136136

137137
```js
138138
// Unstable syntax! It might change or break in production.
139-
@connect(mapState)
139+
@connect(mapStateToProps)
140140
export default class CounterContainer { ... }
141141
```
142142

@@ -220,27 +220,27 @@ React.render(
220220
);
221221
```
222222

223-
### `connect([mapState], [mapDispatch], [mergeProps])`
223+
### `connect([mapStateToProps], [mapDispatchToProps], [mergeProps])`
224224

225225
Connects a React component to a Redux store.
226226

227227
#### Arguments
228228

229-
* [`mapState`] \(*Function*): If specified, the component will subscribe to Redux store updates. Any time it updates, `mapState` will be called. Its result must be a plain object, and it will be merged into the component’s props. If you omit it, the component will not be subscribed to the Redux store.
229+
* [`mapStateToProps(state): stateProps`] \(*Function*): If specified, the component will subscribe to Redux store updates. Any time it updates, `mapStateToProps` will be called. Its result must be a plain object, and it will be merged into the component’s props. If you omit it, the component will not be subscribed to the Redux store.
230230

231-
* [`mapDispatch`] \(*Object* or *Function*): If an object is passed, each function inside it will be assumed to be a Redux action creator. An object with the same function names, but bound to a Redux store, will be merged into the component’s props. If a function is passed, it will be given `dispatch`. It’s up to you to return an object that somehow uses `dispatch` to bind action creators in your own way. (Tip: you may use [`bindActionCreators()`](http://gaearon.github.io/redux/docs/api/bindActionCreators.html) helper from Redux.) If you omit it, the default implementation just injects `dispatch` into your component’s props.
231+
* [`mapDispatchToProps(dispatch): dispatchProps`] \(*Object* or *Function*): If an object is passed, each function inside it will be assumed to be a Redux action creator. An object with the same function names, but bound to a Redux store, will be merged into the component’s props. If a function is passed, it will be given `dispatch`. It’s up to you to return an object that somehow uses `dispatch` to bind action creators in your own way. (Tip: you may use [`bindActionCreators()`](http://gaearon.github.io/redux/docs/api/bindActionCreators.html) helper from Redux.) If you omit it, the default implementation just injects `dispatch` into your component’s props.
232232

233-
* [`mergeProps`] \(*Function*): If specified, it is passed the result of `mapState()`, `mapDispatch()`, and the parent `props`. The plain object you return from it will be passed as props to the wrapped component. You may specify this function to select a slice of the state based on props, or to bind action creators to a particular variable from props. If you omit it, `{ ...props, ...mapStateResult, ...mapDispatchResult }` is used by default.
233+
* [`mergeProps(stateProps, dispatchProps, parentProps): props`] \(*Function*): If specified, it is passed the result of `mapStateToProps()`, `mapDispatchToProps()`, and the parent `props`. The plain object you return from it will be passed as props to the wrapped component. You may specify this function to select a slice of the state based on props, or to bind action creators to a particular variable from props. If you omit it, `{ ...parentProps, ...stateProps, ...dispatchProps }` is used by default.
234234

235235
#### Returns
236236

237237
A React component class that injects state and action creators into your component according to the specified options.
238238

239239
#### Remarks
240240

241-
* It needs to be invoked two times. First time with its arguments described above, and second time, with the component: `connect(mapState, mapDispatch, mergeProps)(MyComponent)`.
241+
* It needs to be invoked two times. First time with its arguments described above, and second time, with the component: `connect(mapStateToProps, mapDispatchToProps, mergeProps)(MyComponent)`.
242242

243-
* The `mapState` function takes a single argument of the entire Redux store’s state and returns an object to be passed as props. It is often called a **selector**. Use [reselect](https://github.com/faassen/reselect) to efficiently compose selectors and [compute derived data](http://gaearon.github.io/redux/docs/recipes/ComputingDerivedData.html).
243+
* The `mapStateToProps` function takes a single argument of the entire Redux store’s state and returns an object to be passed as props. It is often called a **selector**. Use [reselect](https://github.com/faassen/reselect) to efficiently compose selectors and [compute derived data](http://gaearon.github.io/redux/docs/recipes/ComputingDerivedData.html).
244244

245245
* **To use `connect()`, the root component of your app must be wrapped into `<Provider>{() => ... }</Provider>` before being rendered.**
246246

@@ -260,23 +260,23 @@ export default connect(state => state)(TodoApp);
260260
##### Inject `dispatch` and `todos`
261261

262262
```js
263-
function mapState(state) {
263+
function mapStateToProps(state) {
264264
return { todos: state.todos };
265265
}
266266

267-
export default connect(mapState)(TodoApp);
267+
export default connect(mapStateToProps)(TodoApp);
268268
```
269269

270270
##### Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...)
271271

272272
```js
273273
import * as actionCreators from './actionCreators';
274274

275-
function mapState(state) {
275+
function mapStateToProps(state) {
276276
return { todos: state.todos };
277277
}
278278

279-
export default connect(mapState, actionCreators)(TodoApp);
279+
export default connect(mapStateToProps, actionCreators)(TodoApp);
280280
```
281281

282282
##### Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...) as `actions`
@@ -285,15 +285,15 @@ export default connect(mapState, actionCreators)(TodoApp);
285285
import * as actionCreators from './actionCreators';
286286
import { bindActionCreators } from 'redux';
287287

288-
function mapState(state) {
288+
function mapStateToProps(state) {
289289
return { todos: state.todos };
290290
}
291291

292-
function mapDispatch(dispatch) {
292+
function mapDispatchToProps(dispatch) {
293293
return { actions: bindActionCreators(actionCreators, dispatch) };
294294
}
295295

296-
export default connect(mapState, mapDispatch)(TodoApp);
296+
export default connect(mapStateToProps, mapDispatchToProps)(TodoApp);
297297
```
298298

299299
##### Inject `todos` and a specific action creator (`addTodo`)
@@ -302,15 +302,15 @@ export default connect(mapState, mapDispatch)(TodoApp);
302302
import { addTodo } from './actionCreators';
303303
import { bindActionCreators } from 'redux';
304304

305-
function mapState(state) {
305+
function mapStateToProps(state) {
306306
return { todos: state.todos };
307307
}
308308

309-
function mapDispatch(dispatch) {
309+
function mapDispatchToProps(dispatch) {
310310
return { addTodo: bindActionCreators(addTodo, dispatch) };
311311
}
312312

313-
export default connect(mapState, mapDispatch)(TodoApp);
313+
export default connect(mapStateToProps, mapDispatchToProps)(TodoApp);
314314
```
315315

316316
##### Inject `todos`, todoActionCreators as `todoActions`, and counterActionCreators as `counterActions`
@@ -320,18 +320,18 @@ import * as todoActionCreators from './todoActionCreators';
320320
import * as counterActionCreators from './counterActionCreators';
321321
import { bindActionCreators } from 'redux';
322322

323-
function mapState(state) {
323+
function mapStateToProps(state) {
324324
return { todos: state.todos };
325325
}
326326

327-
function mapDispatch(dispatch) {
327+
function mapDispatchToProps(dispatch) {
328328
return {
329329
todoActions: bindActionCreators(todoActionCreators, dispatch),
330330
counterActions: bindActionCreators(counterActionCreators, dispatch)
331331
};
332332
}
333333

334-
export default connect(mapState, mapDispatch)(TodoApp);
334+
export default connect(mapStateToProps, mapDispatchToProps)(TodoApp);
335335
```
336336

337337
##### Inject `todos`, and todoActionCreators and counterActionCreators together as `actions`
@@ -341,17 +341,17 @@ import * as todoActionCreators from './todoActionCreators';
341341
import * as counterActionCreators from './counterActionCreators';
342342
import { bindActionCreators } from 'redux';
343343

344-
function mapState(state) {
344+
function mapStateToProps(state) {
345345
return { todos: state.todos };
346346
}
347347

348-
function mapDispatch(dispatch) {
348+
function mapDispatchToProps(dispatch) {
349349
return {
350350
actions: bindActionCreators({ ...todoActionCreators, ...counterActionCreators }, dispatch)
351351
};
352352
}
353353

354-
export default connect(mapState, mapDispatch)(TodoApp);
354+
export default connect(mapStateToProps, mapDispatchToProps)(TodoApp);
355355
```
356356

357357
##### Inject `todos`, and all todoActionCreators and counterActionCreators directly as props
@@ -361,34 +361,34 @@ import * as todoActionCreators from './todoActionCreators';
361361
import * as counterActionCreators from './counterActionCreators';
362362
import { bindActionCreators } from 'redux';
363363

364-
function mapState(state) {
364+
function mapStateToProps(state) {
365365
return { todos: state.todos };
366366
}
367367

368-
function mapDispatch(dispatch) {
368+
function mapDispatchToProps(dispatch) {
369369
return bindActionCreators(Object.assign({}, todoActionCreators, counterActionCreators), dispatch);
370370
}
371371

372-
export default connect(mapState, mapDispatch)(TodoApp);
372+
export default connect(mapStateToProps, mapDispatchToProps)(TodoApp);
373373
```
374374

375375
##### Inject `todos` of a specific user depending on props, and inject `props.userId` into the action
376376

377377
```js
378378
import * as actionCreators from './actionCreators';
379379

380-
function mapState(state) {
380+
function mapStateToProps(state) {
381381
return { todos: state.todos };
382382
}
383383

384-
function mergeProps(selectedState, boundActions, props) {
385-
return Object.assign({}, props, {
386-
todos: selectedState.todos[props.userId],
387-
addTodo: (text) => boundActions.addTodo(props.userId, text)
384+
function mergeProps(stateProps, dispatchProps, parentProps) {
385+
return Object.assign({}, parentProps, {
386+
todos: stateProps.todos[parentProps.userId],
387+
addTodo: (text) => dispatchProps.addTodo(parentProps.userId, text)
388388
});
389389
}
390390

391-
export default connect(mapState, actionCreators, mergeProps)(TodoApp);
391+
export default connect(mapStateToProps, actionCreators, mergeProps)(TodoApp);
392392
```
393393

394394
## License

0 commit comments

Comments
 (0)