Skip to content

Commit 3079d00

Browse files
committed
Update README.md
1 parent 6e99536 commit 3079d00

File tree

1 file changed

+107
-13
lines changed

1 file changed

+107
-13
lines changed

README.md

Lines changed: 107 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ For React Native, import from `react-redux/native` instead.
1212

1313
- [Quick Start](#quick-start)
1414
- [API](#api)
15-
- [`connect`](#connect)
16-
- [`Provider`](#provider)
15+
- [`connect([mapState], [mapDispatch], [mergeProps])`](#connect)
16+
- [`<Provider store>`](#provider)
1717
- [License](#license)
1818

1919
## Quick Start
@@ -89,7 +89,7 @@ import Counter from '../components/Counter';
8989
// Assuming these are Redux action creators
9090
import { increment } from './actionCreators';
9191

92-
function select(state) {
92+
function mapState(state) {
9393
// Which part of the Redux global state does our component want to receive as props?
9494
return {
9595
counter: state.counter
@@ -99,7 +99,7 @@ function select(state) {
9999
class CounterContainer extends Component {
100100
render() {
101101
// connect() call below will inject `dispatch` and
102-
// every key returned by `select` as props into our container:
102+
// every key returned by `mapState` as props into our container:
103103
const { dispatch, counter } = this.props;
104104

105105
// render our “dumb” component, hooking up state to data props
@@ -114,13 +114,13 @@ class CounterContainer extends Component {
114114
}
115115

116116
// Don't forget to actually use connect!
117-
export default connect(select)(CounterContainer);
117+
export default connect(mapState)(CounterContainer);
118118

119119
// You might have noticed that we used parens twice.
120120
// This is called partial applications, and it lets people
121121
// use ES7 decorator proposal syntax:
122122
//
123-
// @connect(select)
123+
// @connect(mapState)
124124
// export default class CounterContainer { ... }
125125
//
126126
// Don’t forget decorators are experimental! And they
@@ -144,7 +144,7 @@ import { bindActionCreators } from 'redux';
144144
import * as CounterActionCreators from './actionCreators';
145145
import Counter from '../components/Counter';
146146

147-
function select(state) {
147+
function mapState(state) {
148148
return {
149149
counter: state.counter
150150
};
@@ -165,7 +165,7 @@ class CounterContainer extends Component {
165165
}
166166

167167
// Don't forget to actually use connect!
168-
export default connect(select)(CounterContainer);
168+
export default connect(mapState)(CounterContainer);
169169
```
170170

171171
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.
@@ -216,17 +216,110 @@ React.render((
216216

217217
## API
218218

219-
### `connect`
219+
### `connect([mapState], [mapDispatch], [mergeProps])`
220220

221221
```js
222-
export default connect(select)(MyComponent);
222+
// Inject just `dispatch` and don't listen to store
223+
export default connect()(TodoApp);
224+
225+
// Inject `dispatch` and every field in the global state (SLOW!)
226+
export default connect(state => state)(TodoApp);
227+
228+
// Inject `dispatch` and `todos`
229+
function mapState(state) {
230+
return { todos: state.todos };
231+
}
232+
export default connect(mapState)(TodoApp);
233+
234+
// Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...)
235+
import * as actionCreators from './actionCreators';
236+
function mapState(state) {
237+
return { todos: state.todos };
238+
}
239+
export default connect(mapState, actionCreators)(TodoApp);
240+
241+
// Inject `todos` and all action creators (`addTodo`, `completeTodo`, ...) as `actions`
242+
import * as actionCreators from './actionCreators';
243+
import { bindActionCreators } from 'redux';
244+
function mapState(state) {
245+
return { todos: state.todos };
246+
}
247+
function mapDispatch(dispatch) {
248+
return { actions: bindActionCreators(actionCreators, dispatch) };
249+
}
250+
export default connect(mapState, actionCreators)(TodoApp);
251+
252+
// Inject `todos` and a specific action creator (`addTodo`)
253+
import { addTodo } from './actionCreators';
254+
import { bindActionCreators } from 'redux';
255+
function mapState(state) {
256+
return { todos: state.todos };
257+
}
258+
function mapDispatch(dispatch) {
259+
return { addTodo: bindActionCreators(addTodo, dispatch) };
260+
}
261+
export default connect(mapState, mapDispatch)(TodoApp);
262+
263+
// Inject `todos`, todoActionCreators as `todoActions`, and counterActionCreators as `counterActions`
264+
import * as todoActionCreators from './todoActionCreators';
265+
import * as counterActionCreators from './counterActionCreators';
266+
import { bindActionCreators } from 'redux';
267+
function mapState(state) {
268+
return { todos: state.todos };
269+
}
270+
function mapDispatch(dispatch) {
271+
return {
272+
todoActions: bindActionCreators(todoActionCreators, dispatch),
273+
counterActions: bindActionCreators(counterActionCreators, dispatch)
274+
};
275+
}
276+
export default connect(mapState, mapDispatch)(TodoApp);
277+
278+
// Inject `todos`, and todoActionCreators and counterActionCreators together as `actions`
279+
import * as todoActionCreators from './todoActionCreators';
280+
import * as counterActionCreators from './counterActionCreators';
281+
import { bindActionCreators } from 'redux';
282+
function mapState(state) {
283+
return { todos: state.todos };
284+
}
285+
function mapDispatch(dispatch) {
286+
return {
287+
actions: bindActionCreators({ ...todoActionCreators, ...counterActionCreators }, dispatch)
288+
};
289+
}
290+
export default connect(mapState, mapDispatch)(TodoApp);
291+
292+
// Inject `todos`, and all todoActionCreators and counterActionCreators directly as props
293+
import * as todoActionCreators from './todoActionCreators';
294+
import * as counterActionCreators from './counterActionCreators';
295+
import { bindActionCreators } from 'redux';
296+
function mapState(state) {
297+
return { todos: state.todos };
298+
}
299+
function mapDispatch(dispatch) {
300+
return bindActionCreators({ ...todoActionCreators, ...counterActionCreators }, dispatch);
301+
}
302+
export default connect(mapState, mapDispatch)(TodoApp);
303+
304+
// Inject `todos` of a specific user depending on props, and inject `props.userId` into the action
305+
import * as actionCreators from './actionCreators';
306+
function mapState(state) {
307+
return { todos: state.todos };
308+
}
309+
function mergeProps(selectedState, boundActions, props) {
310+
return Object.assign({}, props, {
311+
todos: selectedState.todos[props.userId],
312+
addTodo: (text) => boundActions.addTodo(props.userId, text)
313+
});
314+
}
315+
export default connect(mapState, actionCreators)(TodoApp);
223316
```
224317

225318
Returns a component class that injects the Redux Store’s `dispatch` as a prop into `Component` so it can dispatch Redux actions.
226319

227-
The returned component also subscribes to the updates of Redux store. Any time the state changes, it calls the `select` function passed to it. 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.
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.
228321

229-
Both `dispatch` and every property returned by `select` will be provided to your `Component` as `props`.
322+
Both `dispatch` and every property returned by `mapState` will be provided to your `Component` as `props`.
230323

231324
It is the responsibility of a Smart Component to bind action creators to the given `dispatch` function and pass those
232325
bound creators to Dumb Components. Redux provides a `bindActionCreators` to streamline the process of binding action
@@ -236,9 +329,10 @@ creators to the dispatch function.
236329

237330
See the usage example in the quick start above.
238331

239-
### `Provider`
332+
### `<Provider store>`
240333

241334
```js
335+
// Make store available to connect() below in hierarchy
242336
<Provider store={store}>
243337
{() => <MyRootComponent>}
244338
</Provider>

0 commit comments

Comments
 (0)