|
1 | 1 | react-redux
|
2 | 2 | =========================
|
3 | 3 |
|
4 |
| -React bindings for Redux. |
5 |
| -Docs are work in progress. |
| 4 | +Higher-order React components for [Redux](https://github.com/gaearon/redux). |
6 | 5 |
|
7 |
| -Requires React >= 0.13 |
| 6 | +- [Quick Start](#quick-start) |
| 7 | +- [Recommended API](#recommended-api) |
| 8 | + - [`connect`](#connect) |
| 9 | + - [`Provider`](#provider) |
| 10 | +- [Deprecated API](#deprecated-api) |
| 11 | + - [`Connector`](#connector) |
| 12 | + - [`provide`](#provide) |
| 13 | + |
| 14 | +## Quick Start |
| 15 | + |
| 16 | +React bindings for Redux embrace the idea of [dividing “smart” and “dumb” components](https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0). |
| 17 | + |
| 18 | +It is advisable that only top-level components of your app (such as route handlers, for example) are aware of Redux. Components below them should be “dumb” and receive all data via props. |
| 19 | + |
| 20 | +<center> |
| 21 | +<table> |
| 22 | + <thead> |
| 23 | + <tr> |
| 24 | + <th></th> |
| 25 | + <th>Location</th> |
| 26 | + <th>Use React-Redux</th> |
| 27 | + <th>To read data, they</th> |
| 28 | + <th>To change data, they</th> |
| 29 | + </tr> |
| 30 | + </thead> |
| 31 | + <tbody> |
| 32 | + <tr> |
| 33 | + <td>“Smart” Components</td> |
| 34 | + <td>Top level, route handlers</td> |
| 35 | + <td>Yes</th> |
| 36 | + <td>Subscribe to Redux state</td> |
| 37 | + <td>Dispatch Redux actions</td> |
| 38 | + </tr> |
| 39 | + <tr> |
| 40 | + <td>“Dumb” Components</td> |
| 41 | + <td>Middle and leaf components</td> |
| 42 | + <td>No</th> |
| 43 | + <td>Read data from props</td> |
| 44 | + <td>Invoke callbacks from props</td> |
| 45 | + </tr> |
| 46 | + </tbody> |
| 47 | +</table> |
| 48 | +</center> |
| 49 | + |
| 50 | +### “Dumb” component is unaware of Redux |
| 51 | + |
| 52 | +Let’s say we have a `<Counter />` “dumb” component with a number `counter` prop, and an `increment` function prop that it will call when user presses an “Increment” button: |
| 53 | + |
| 54 | +```js |
| 55 | +import { Component } from 'react'; |
| 56 | + |
| 57 | +export default class Counter extends Component { |
| 58 | + render() { |
| 59 | + return ( |
| 60 | + <button onClick={this.props.increment}> |
| 61 | + {this.props.counter} |
| 62 | + </button> |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### “Smart” component is `connect()`-ed to Redux |
| 69 | + |
| 70 | +Here’s how we hook it up to the Redux Store. |
| 71 | + |
| 72 | +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. |
| 73 | + |
| 74 | +Our `CounterContainer` that’s necessary to hook `Counter` up to a Redux store looks like this: |
| 75 | +(This will be much less verbose in the next versions.) |
| 76 | + |
| 77 | +```js |
| 78 | +import { Component } from 'react'; |
| 79 | +import { connect } from 'react-redux'; |
| 80 | + |
| 81 | +// Assuming this is our “dumb” counter |
| 82 | +import Counter from '../components/Counter'; |
| 83 | + |
| 84 | +// Assuming these are Redux action creators |
| 85 | +import { increment } from './actionCreators'; |
| 86 | + |
| 87 | +function select(state) { |
| 88 | + // Which part of the Redux global state does our component want to receive as props? |
| 89 | + return { |
| 90 | + counter: state.counter |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +class CounterContainer extends Component { |
| 95 | + render() { |
| 96 | + // connect() call below will inject `dispatch` and |
| 97 | + // every key returned by `select` as props into our container: |
| 98 | + const { dispatch, counter } = this.props; |
| 99 | + |
| 100 | + // render our “dumb” component, hooking up state to data props |
| 101 | + // and using “dispatch action produced by this action creator” as callbacks. |
| 102 | + // this is a “bridge” between a Redux-aware world above and Redux-unaware world below. |
| 103 | + |
| 104 | + return ( |
| 105 | + <Counter counter={counter} |
| 106 | + increment={() => dispatch(increment())} /> |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Don't forget to actually use connect! |
| 112 | +export default connect(select)(CounterContainer); |
| 113 | + |
| 114 | +// You might have noticed that we used parens twice. |
| 115 | +// This is called partial applications, and it lets people |
| 116 | +// use ES7 decorator proposal syntax: |
| 117 | +// |
| 118 | +// @connect(select) |
| 119 | +// export default class CounterContainer { ... } |
| 120 | +// |
| 121 | +// Don’t forget decorators are experimental! And they |
| 122 | +// desugar to function calls anyway as example above demonstrates. |
| 123 | +``` |
| 124 | + |
| 125 | +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! |
| 126 | + |
| 127 | +### Binding many action creators |
| 128 | + |
| 129 | +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: |
| 130 | + |
| 131 | +```js |
| 132 | + |
| 133 | +import { Component } from 'react'; |
| 134 | +import { connect } from 'react-redux'; |
| 135 | + |
| 136 | +// A helper provided by Redux! |
| 137 | +import { bindActionCreators } from 'redux'; |
| 138 | +// Import many action creators as a single object (like `require('./actionCreators')` in CommonJS) |
| 139 | +import * as CounterActionCreators from './actionCreators'; |
| 140 | +import Counter from '../components/Counter'; |
| 141 | + |
| 142 | +function select(state) { |
| 143 | + return { |
| 144 | + counter: state.counter |
| 145 | + }; |
| 146 | +} |
| 147 | + |
| 148 | +class CounterContainer extends Component { |
| 149 | + render() { |
| 150 | + const { dispatch, counter } = this.props; |
| 151 | + |
| 152 | + // This time, we use `bindActionCreators` to bind many action creators |
| 153 | + // to a particular dispatch function from our Redux store. |
| 154 | + |
| 155 | + return ( |
| 156 | + <Counter counter={counter} |
| 157 | + {...bindActionCreators(CounterActionCreators, dispatch)} /> |
| 158 | + ); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// Don't forget to actually use connect! |
| 163 | +export default connect(select)(CounterContainer); |
| 164 | +``` |
| 165 | + |
| 166 | +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. |
| 167 | + |
| 168 | +### Injecting Redux store |
| 169 | + |
| 170 | +Finally, how do we actually hook it up to a Redux store? We need to create the store somewhere at the root of our component hierarchy. For client apps, the root component is a good place. For server rendering, you can do this in the request handler. |
| 171 | + |
| 172 | +The trick is to wrap the whole view hierarchy into `<Provider>{() => ... }</Provider>` where `Provider` is imported from `react-redux`. One gotcha is that **the child of `Provider` must be a function**. This is to work around an issue with how context (undocumented feature we have to rely on to pass Redux data to components below) works in React 0.13. In React 0.14, you will be able to put your view hierarchy in `<Provider>` without wrapping it into a function. |
| 173 | + |
| 174 | +```js |
| 175 | +import { Component } from 'react'; |
| 176 | +import { Provider } from 'react-redux'; |
| 177 | + |
| 178 | +class App extends Component { |
| 179 | + render() { |
| 180 | + // ... |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +const targetEl = document.getElementById('root'); |
| 185 | + |
| 186 | +React.render(( |
| 187 | + <Provider store={store}> |
| 188 | + {() => <App />} |
| 189 | + </Provider> |
| 190 | +), targetEl); |
| 191 | + |
| 192 | +// or, if you use React Router 0.13, |
| 193 | + |
| 194 | +// Router.run(routes, Router.HistoryLocation, (Handler) => { |
| 195 | +// React.render( |
| 196 | +// <Provider store={store}> |
| 197 | +// {() => <Handler />} |
| 198 | +// </Provider>, |
| 199 | +// targetEl |
| 200 | +// ); |
| 201 | +// }); |
| 202 | + |
| 203 | +// or, if you use React Router 1.0, |
| 204 | +// React.render( |
| 205 | +// <Provider store={store}> |
| 206 | +// {() => <Router history={history}>...</Router>} |
| 207 | +// </Provider>, |
| 208 | +// targetEl |
| 209 | +// ); |
| 210 | +``` |
| 211 | + |
| 212 | +## Recommended API |
| 213 | + |
| 214 | +### `connect` |
| 215 | + |
| 216 | +```js |
| 217 | +export default connect(select)(MyComponent); |
| 218 | +``` |
| 219 | + |
| 220 | +Returns a component class that injects the Redux Store’s `dispatch` as a prop into `Component` so it can dispatch Redux actions. |
| 221 | + |
| 222 | +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. |
| 223 | + |
| 224 | +Both `dispatch` and every property returned by `select` will be provided to your `Component` as `props`. |
| 225 | + |
| 226 | +It is the responsibility of a Smart Component to bind action creators to the given `dispatch` function and pass those |
| 227 | +bound creators to Dumb Components. Redux provides a `bindActionCreators` to streamline the process of binding action |
| 228 | +creators to the dispatch function. |
| 229 | + |
| 230 | +**To use `connect()`, the root component of your app must be wrapped into `<Provider>{() => ... }</Provider>` before being rendered.** |
| 231 | + |
| 232 | +See the usage example in the quick start above. |
| 233 | + |
| 234 | +### `Provider` |
| 235 | + |
| 236 | +```js |
| 237 | +<Provider store={store}> |
| 238 | + {() => <MyRootComponent>} |
| 239 | +</Provider> |
| 240 | +``` |
| 241 | + |
| 242 | +The `Provider` component takes a `store` prop and a [function as a child](#child-must-be-a-function) with your root |
| 243 | +component. The `store` is then passed to the child via React's `context`. This is the entry point for Redux and must be |
| 244 | +present in order to use the `Connector` component. |
| 245 | + |
| 246 | +## Deprecated API |
| 247 | + |
| 248 | +### `Connector` |
| 249 | + |
| 250 | +>**Note** |
| 251 | +>Deprecated. Use `connect()` instead. |
| 252 | +
|
| 253 | +```js |
| 254 | +<Connector select={fn}> |
| 255 | + {props => <MyComponent {...props} />} |
| 256 | +</Connector> |
| 257 | +``` |
| 258 | + |
| 259 | +Similar to `Provider`, the `Connector` expects a single [function as a child](#child-must-be-a-function) and a function |
| 260 | +as the `select` prop. The selector function takes a single argument of the entire root store and returns an object to be |
| 261 | +passed as properties to the child. In addition to the properties returned by the selector, a `dispatch` function is |
| 262 | +passed to the child for dispatching actions. |
| 263 | + |
| 264 | +It is the responsibility of a Smart Component to bind action creators to the given `dispatch` function and pass those |
| 265 | +bound creators to Dumb Components. Redux provides a `bindActionCreators` to streamline the process of binding action |
| 266 | +creators to the dispatch function. |
| 267 | + |
| 268 | +We don’t recommend its use anymore because it’s not as flexible as `connect()` and has some performance implications for more complex scenarios. |
| 269 | + |
| 270 | +### `provide` |
| 271 | + |
| 272 | +>**Note** |
| 273 | +>Deprecated. Use `<Provider>` instead. |
| 274 | +
|
| 275 | +```js |
| 276 | +export default provide(store)(MyRootComponent); |
| 277 | +``` |
| 278 | + |
| 279 | +This higher-order component provides the same functionality as `<Provider>`. We don’t recommend it anymore because it’s less flexible than `<Provider>` and doesn’t work with [redux-devtools](http://github.com/gaearon/redux-devtools) or server rendering. |
0 commit comments