|
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 | +- [Components](#components) |
| 8 | + - [`Provider`](#provider) |
| 9 | + - [`Connector`](#connector) |
| 10 | +- [Decorators](#decorators) |
| 11 | + |
| 12 | +## Quick Start |
| 13 | + |
| 14 | +Hooking up the Counter example into React looks like this: |
| 15 | + |
| 16 | +```js |
| 17 | +import { bindActionCreators } from 'redux'; |
| 18 | +import { Provider, Connector } from 'react-redux'; |
| 19 | + |
| 20 | +// store setup left out... see the Redux documentation for initializing action creators, reducers and the store. |
| 21 | + |
| 22 | +function select(state) { |
| 23 | + return { counter: state.counter }; |
| 24 | +} |
| 25 | +class CounterApp { |
| 26 | + render() { |
| 27 | + return ( |
| 28 | + <Connector select={select}> |
| 29 | + {({ counter, dispatch }) => |
| 30 | + /* Yes this is child as a function. See the Connector section for why this is. */ |
| 31 | + <Counter counter={counter} |
| 32 | + {...bindActionCreators(CounterActions, dispatch)} /> |
| 33 | + } |
| 34 | + </Connector> |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +const containerElement = document.getElementByID('container'); |
| 40 | +React.run(( |
| 41 | + <Provider store={store}> |
| 42 | + {() => <CounterApp />} |
| 43 | + </Provider> |
| 44 | +), containerElement); |
| 45 | +``` |
| 46 | + |
| 47 | +## Components |
| 48 | + |
| 49 | +### `Provider` |
| 50 | + |
| 51 | +The `Provider` component takes a `store` prop and a [function as a child](#child-must-be-a-function) with your root |
| 52 | +component. The `store` is then passed to the child via React's `context`. This is the entry point for Redux and must be |
| 53 | +present in order to use the `Connector` component. |
| 54 | + |
| 55 | +### `Connector` |
| 56 | + |
| 57 | +Components in React can be divided into two categories: Dumb Components, which have no knowledge of your application's |
| 58 | +state and focus on a specific piece of functionality; and Smart Components which take in your application's state |
| 59 | +and stich together Dumb Components. This library has a Higher-Order Component called `Connector` for providing specific |
| 60 | +pieces of state to a Smart Component automatically and handling store changes gracefully. |
| 61 | + |
| 62 | +Similar to `Provider`, the `Connector` expects a single [function as a child](#child-must-be-a-function) and a function |
| 63 | +as the `select` prop. The selector function takes a single argument of the entire root store and returns an object to be |
| 64 | +passed as properties to the child. In addition to the properties returned by the selector, a `dispatch` function is |
| 65 | +passed to the child for dispatching actions. |
| 66 | + |
| 67 | +It is the responsibility of a Smart Component to bind action creators to the given `dispatch` function and pass those |
| 68 | +bound creators to Dumb Components. Redux provides a `bindActionCreators` to streamline the process of binding action |
| 69 | +creators to the dispatch function. |
| 70 | + |
| 71 | +#### Child must be a function |
| 72 | + |
| 73 | +React's context feature is technically not feature complete and has a bug in the current (0.13) release. The work around |
| 74 | +for this bug is to pass a function as the child which will then return the Component desired. |
| 75 | + |
| 76 | +## Decorators |
| 77 | + |
| 78 | +ECMA Script 6 introduces a new syntactic feature called Decorators. `react-redux` comes with two decorators to simply |
| 79 | +creating smart components and providing the store at the top level. Here is the same example at the top of this document |
| 80 | +using decorators: |
| 81 | + |
| 82 | +```js |
| 83 | +import { bindActionCreators } from 'redux'; |
| 84 | +import { connect, provide } from `react-redux'; |
| 85 | +
|
| 86 | +// store setup left out... see the Redux documentation for initializing action creators, reducers and the store. |
| 87 | +
|
| 88 | +// Note: you do *not* have to `@provide` every component you `@connect`, but this abritrarily simple example only has |
| 89 | +one Smart Component at the top level. A more complete example may have a root level component that is only decorated |
| 90 | +with `@provide` and many smart components decorated with `@connect`. |
| 91 | +@provide(store) |
| 92 | +@connect((state) => ({ counter: state.counter })) |
| 93 | +class CounterApp { |
| 94 | + render() { |
| 95 | + return <Counter counter={counter} {...bindActionCreators(CounterActions, dispatch)} />; |
| 96 | + } |
| 97 | +} |
| 98 | +
|
| 99 | +const containerElement = document.getElementByID('container'); |
| 100 | +React.run(<CounterApp />, containerElement); |
| 101 | +``` |
0 commit comments