You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+2-3
Original file line number
Diff line number
Diff line change
@@ -15,12 +15,11 @@ Performant and flexible.
15
15
npm install --save react-redux
16
16
```
17
17
18
-
React Redux requires **React 0.13 or later.**
18
+
React Redux requires **React 0.14 or later.**
19
19
20
20
## React Native
21
21
22
-
What you get from `react-redux` is for React.
23
-
For React Native, import from `react-redux/native` instead.
22
+
Until [React Native is compatible with React 0.14](https://github.com/facebook/react-native/issues/2985), you’ll need to keep using [React Redux 3.x branch and documentation](https://github.com/rackt/react-redux/tree/v3.1.0).
Copy file name to clipboardExpand all lines: docs/api.md
+30-14
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,23 @@
2
2
3
3
### `<Provider store>`
4
4
5
-
Makes the Redux store available to the `connect()` calls in the component hierarchy below. Normally, you can’t use `connect()` without wrapping the root component in `<Provider>`. (If you *really* need to, you can manually pass `store` as a prop to every `connect()`ed component, but we only recommend to do this for stubbing `store` in unit tests, or in non-fully-React codebases. Normally, you should just use `<Provider>`.)
5
+
Makes the Redux store available to the `connect()` calls in the component hierarchy below. Normally, you can’t use `connect()` without wrapping the root component in `<Provider>`.
6
+
7
+
If you *really* need to, you can manually pass `store` as a prop to every `connect()`ed component, but we only recommend to do this for stubbing `store` in unit tests, or in non-fully-React codebases. Normally, you should just use `<Provider>`.
6
8
7
9
#### Props
8
10
9
-
*`store`: (*[Redux Store](http://gaearon.github.io/redux/docs/api/Store.html)*): The single Redux store in your application.
10
-
*`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.
11
+
*`store` (*[Redux Store](http://rackt.github.io/redux/docs/api/Store.html)*): The single Redux store in your application.
12
+
*`children` (*ReactElement*) The root of your component hierarchy.
11
13
12
14
#### Example
13
15
14
16
##### Vanilla React
15
17
16
18
```js
17
-
React.render(
19
+
ReactDOM.render(
18
20
<Provider store={store}>
19
-
{() =><MyRootComponent />}
21
+
<MyRootComponent />
20
22
</Provider>,
21
23
rootEl
22
24
);
@@ -26,9 +28,10 @@ React.render(
26
28
27
29
```js
28
30
Router.run(routes, Router.HistoryLocation, (Handler, routerState) => { // note "routerState" here
29
-
React.render(
31
+
ReactDOM.render(
30
32
<Provider store={store}>
31
-
{() =><Handler routerState={routerState} />} // note "routerState" here: important to pass it down
33
+
{/* note "routerState" here: important to pass it down */}
@@ -57,26 +60,39 @@ Instead, it *returns* a new, connected component class, for you to use.
57
60
58
61
*[`mapStateToProps(state, [ownProps]): 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. If `ownProps` is specified as a second argument, its value will be the properties passed to your component, and `mapStateToProps` will be re-invoked whenever the component receives new props.
59
62
60
-
*[`mapDispatchToProps(dispatch, [ownProps]): 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 the [`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. If `ownProps` is specified as a second argument, its value will be the properties passed to your component, and `mapDispatchToProps` will be re-invoked whenever the component receives new props.
63
+
*[`mapDispatchToProps(dispatch, [ownProps]): 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 the [`bindActionCreators()`](http://rackt.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. If `ownProps` is specified as a second argument, its value will be the properties passed to your component, and `mapDispatchToProps` will be re-invoked whenever the component receives new props.
61
64
62
65
*[`mergeProps(stateProps, dispatchProps, ownProps): 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, `Object.assign({}, ownProps, stateProps, dispatchProps)` is used by default.
63
66
64
67
*[`options`]*(Object)* If specified, further customizes the behavior of the connector.
65
-
*[`pure`]*(Boolean)*: If true, implements `shouldComponentUpdate` and shallowly compares the result of `mergeProps`, preventing unnecessary updates, assuming that the component is a “pure” component and does not rely on any input or state other than its props and the selected Redux store’s state. *Defaults to `true`.*
68
+
*[`pure = true`]*(Boolean)*: If true, implements `shouldComponentUpdate` and shallowly compares the result of `mergeProps`, preventing unnecessary updates, assuming that the component is a “pure” component and does not rely on any input or state other than its props and the selected Redux store’s state. *Defaults to `true`.*
69
+
*[`withRef = false`]*(Boolean)*: If true, stores a ref to the wrapped component instance and makes it available via `getWrappedInstance()` method. *Defaults to `false`.*
66
70
67
71
#### Returns
68
72
69
73
A React component class that injects state and action creators into your component according to the specified options.
70
74
75
+
##### Static Properties
76
+
77
+
*`WrappedComponent`*(Component)*: The original component class passed to `connect()`.
78
+
79
+
##### Static Methods
80
+
81
+
All the original static methods of the component are hoisted.
82
+
83
+
##### Instance Methods
84
+
85
+
###### `getWrappedInstance(): ReactComponent`
86
+
87
+
Returns the wrapped component instance. Only available if you pass `{ withRef: true }` as part of the `connect()`’s fourth `options` argument.
88
+
71
89
#### Remarks
72
90
73
91
* It needs to be invoked two times. The first time with its arguments described above, and a second time, with the component: `connect(mapStateToProps, mapDispatchToProps, mergeProps)(MyComponent)`.
74
92
75
93
* It does not modify the passed React component. It returns a new, connected component, that you should use instead.
76
94
77
-
* 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).
78
-
79
-
***To use `connect()`, the root component of your app must be wrapped into `<Provider>{() => ... }</Provider>` before being rendered.** You may also pass `store` as a prop to the `connect()`ed component, but it's not recommended, because it's just too much trouble. Only do this for non-fully-React codebases or to stub the store in a unit test.
95
+
* 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/rackt/reselect) to efficiently compose selectors and [compute derived data](http://rackt.github.io/redux/docs/recipes/ComputingDerivedData.html).
Copy file name to clipboardExpand all lines: docs/quick-start.md
+7-5
Original file line number
Diff line number
Diff line change
@@ -126,9 +126,10 @@ This is the most basic usage, but `connect()` supports many other patterns: just
126
126
127
127
Finally, how do we actually hook it up to the 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.
128
128
129
-
The trick is to wrap the whole view hierarchy into a `<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 about 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.
129
+
The trick is to wrap the whole view hierarchy into a `<Provider>`from React Redux.
130
130
131
131
```js
132
+
importReactDOMfrom'react-dom';
132
133
import { Component } from'react';
133
134
import { Provider } from'react-redux';
134
135
@@ -140,9 +141,10 @@ class App extends Component {
Copy file name to clipboardExpand all lines: docs/troubleshooting.md
+6-7
Original file line number
Diff line number
Diff line change
@@ -18,9 +18,10 @@ Root view:
18
18
19
19
```js
20
20
Router.run(routes, Router.HistoryLocation, (Handler, routerState) => { // note "routerState" here
21
-
React.render(
21
+
ReactDOM.render(
22
22
<Provider store={store}>
23
-
{() =><Handler routerState={routerState} />} // note "routerState" here
23
+
{/* note "routerState" here */}
24
+
<Handler routerState={routerState} />
24
25
</Provider>,
25
26
document.getElementById('root')
26
27
);
@@ -41,13 +42,13 @@ You can also upgrade to React Router 1.0 which shouldn’t have this problem. (L
41
42
42
43
### My views aren’t updating when something changes outside of Redux
43
44
44
-
If your views depend on global state or [React “context”](www.youtube.com/watch?v=H7vlH-wntD4), you might find that views decorated with `connect()` will fail to update.
45
+
If your views depend on global state or [React “context”](http://facebook.github.io/react/docs/context.html), you might find that views decorated with `connect()` will fail to update.
45
46
46
47
>This is because `connect()` implements [shouldComponentUpdate](https://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate) by default, assuming that your component will produce the same results given the same props and state. This is a similar concept to React’s [PureRenderMixin](https://facebook.github.io/react/docs/pure-render-mixin.html).
47
48
48
49
The _best_ solution to this is to make sure that your components are pure and pass any external state to them via props. This will ensure that your views do not re-render unless they actually need to re-render and will greatly speed up your application.
49
50
50
-
If that's not practical for whatever reason (for example, if you’re using a library that depends heavily on React context), you may pass the `pure: false` option to `connect()`:
51
+
If that’s not practical for whatever reason (for example, if you’re using a library that depends heavily on React context), you may pass the `pure: false` option to `connect()`:
51
52
52
53
```
53
54
function mapStateToProps(state) {
@@ -67,10 +68,8 @@ If you have context issues,
67
68
68
69
1.[Make sure you don’t have a duplicate instance of React](https://medium.com/@dan_abramov/two-weird-tricks-that-fix-react-7cf9bbdef375) on the page.
69
70
2. Make sure you didn’t forget to wrap your root component in [`<Provider>`](#provider-store).
70
-
3.If you use React Router, something like `<Provider>{() => router}</Provider>` won’t work. Due to the way context works in React 0.13, it’s important that the `<Provider>` children are *created* inside that function. Just referencing an outside variable doesn’t do the trick. Instead of `<Provider>{() => router}</Provider>`, write `<Provider>{() => createRouter()}</Provider>` where `createRouter()` is a function that actually *creates* (and returns) the router.
71
+
3.Make sure you’re running the latest versions of React and React Redux.
71
72
72
73
### Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you’re trying to add a ref to a component that doesn’t have an owner
73
74
74
75
If you’re using React for web, this usually means you have a [duplicate React](https://medium.com/@dan_abramov/two-weird-tricks-that-fix-react-7cf9bbdef375). Follow the linked instructions to fix this.
75
-
76
-
If you’re using React Native, make sure you’re importing `react-redux/native` both for `<Provider>` and any `connect()` call. Importing from `react-redux` will not work on React Native.
0 commit comments