Skip to content

Commit 4a3d699

Browse files
committed
Update README.md
1 parent 65ce197 commit 4a3d699

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

README.md

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ Performant and flexible.
1111
- [React Native](#react-native)
1212
- [Quick Start](#quick-start)
1313
- [API](#api)
14-
- [`connect([mapState], [mapDispatch], [mergeProps])`](#connectmapstate-mapdispatch-mergeprops)
1514
- [`<Provider store>`](#provider-store)
16-
- [Recipes](#recipes)
15+
- [`connect([mapState], [mapDispatch], [mergeProps])(Component)`](#connectmapstate-mapdispatch-mergeprops-component)
1716
- [License](#license)
1817

1918
## React Native
@@ -85,6 +84,8 @@ Passing action creator functions as the second parameter will bind them to the s
8584

8685
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!
8786

87+
##### `containers/CounterContainer.js`
88+
8889
```js
8990
import { Component } from 'react';
9091
import { connect } from 'react-redux';
@@ -106,6 +107,9 @@ function mapState(state) {
106107
export default connect(mapState, counterActionCreators)(CounterContainer);
107108
```
108109

110+
Whether to put `connect()` call in the same file as the “dumb” component, or separately, is up to you.
111+
Ask yourself whether you'd want to reuse this component but bind it to different data, or not.
112+
109113
### Usage Notes
110114

111115
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.
@@ -125,7 +129,7 @@ Don’t forget decorators are experimental! And they desugar to function calls a
125129

126130
### Additional Flexibility
127131

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.
132+
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 `connect()` docs below to learn more.
129133

130134
### Injecting Redux store
131135

@@ -173,36 +177,51 @@ React.render((
173177

174178
## API
175179

176-
### `connect([mapState], [mapDispatch], [mergeProps])`
177-
178-
Returns a component class that injects the Redux Store’s `dispatch` as a prop into `Component` so it can dispatch Redux actions.
179-
180-
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.
181-
182-
Both `dispatch` and every property returned by `mapState` will be provided to your `Component` as `props`.
180+
### `<Provider store>`
183181

184-
It is the responsibility of a Smart Component to bind action creators to the given `dispatch` function and pass those
185-
bound creators to Dumb Components. Redux provides a `bindActionCreators` to streamline the process of binding action
186-
creators to the dispatch function.
182+
Makes Redux store available to the `connect()` calls in the component hierarchy below.
183+
You can’t use `connect()` without wrapping the root component in `<Provider>`.
187184

188-
**To use `connect()`, the root component of your app must be wrapped into `<Provider>{() => ... }</Provider>` before being rendered.**
185+
#### Props
189186

190-
See the usage example in the quick start above.
187+
* `store`: (*[Redux Store](http://gaearon.github.io/redux/docs/api/Store.html)*): The single Redux store in your application.
188+
* `children`: (*Function*): Unlike most React components, `<Provider>` accepts a [function as a child](#child-must-be-a-function) with your root component. This is a temporary workaround for a React 0.13 context issue, which will be fixed when React 0.14 comes out.
191189

192-
### `<Provider store>`
190+
#### Example
193191

194192
```js
195193
// Make store available to connect() below in hierarchy
196-
<Provider store={store}>
197-
{() => <MyRootComponent>}
198-
</Provider>
194+
React.render(
195+
<Provider store={store}>
196+
{() => <MyRootComponent>}
197+
</Provider>,
198+
rootEl
199+
);
199200
```
200201

201-
The `Provider` component takes a `store` prop and a [function as a child](#child-must-be-a-function) with your root
202-
component. The `store` is then passed to the child via React's `context`. This is the entry point for Redux and must be
203-
present in order to use the `connect` component.
202+
### `connect([mapState], [mapDispatch], [mergeProps])(Component)`
203+
204+
Connects a React component to a Redux store.
205+
206+
#### Arguments
207+
208+
* [`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.
209+
210+
* [`mapDispatch`] (*Object* or *Function*): If an object is passed, each function inside it will be assumed to be a Redux action creator, and 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` helper from Redux.) If you pass omit it, the default implementation just injects `dispatch` into your component’s props.
211+
212+
* [`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.
213+
214+
#### Returns
215+
216+
A React component class that injects state and action creators into your component according to the specified options.
217+
218+
#### Remarks
219+
220+
* 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).
221+
222+
* **To use `connect()`, the root component of your app must be wrapped into `<Provider>{() => ... }</Provider>` before being rendered.**
204223

205-
## Recipes
224+
#### Examples
206225

207226
##### Inject just `dispatch` and don't listen to store
208227

0 commit comments

Comments
 (0)