diff --git a/CHANGELOG.md b/CHANGELOG.md index ebb8429f2..df3556609 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1 @@ -All notable changes are described on the [Releases](https://github.com/gaearon/react-redux/releases) page. +All notable changes are described on the [Releases](https://github.com/rackt/react-redux/releases) page. diff --git a/README.md b/README.md index 9f776f53b..1668c9ee4 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,11 @@ Performant and flexible. npm install --save react-redux ``` -React Redux requires **React 0.13 or later.** +React Redux requires **React 0.14 or later.** ## React Native -What you get from `react-redux` is for React. -For React Native, import from `react-redux/native` instead. +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). ## Documentation diff --git a/docs/api.md b/docs/api.md index 7667c0599..2670794c5 100644 --- a/docs/api.md +++ b/docs/api.md @@ -2,21 +2,23 @@ ### `` -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 ``. (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 ``.) +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 ``. + +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 ``. #### Props -* `store`: (*[Redux Store](http://gaearon.github.io/redux/docs/api/Store.html)*): The single Redux store in your application. -* `children`: (*Function*): Unlike most React components, `` 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. +* `store` (*[Redux Store](http://rackt.github.io/redux/docs/api/Store.html)*): The single Redux store in your application. +* `children` (*ReactElement*) The root of your component hierarchy. #### Example ##### Vanilla React ```js -React.render( +ReactDOM.render( - {() => } + , rootEl ); @@ -26,9 +28,10 @@ React.render( ```js Router.run(routes, Router.HistoryLocation, (Handler, routerState) => { // note "routerState" here - React.render( + ReactDOM.render( - {() => } // note "routerState" here: important to pass it down + {/* note "routerState" here: important to pass it down */} + , document.getElementById('root') ); @@ -38,9 +41,9 @@ Router.run(routes, Router.HistoryLocation, (Handler, routerState) => { // note " ##### React Router 1.0 ```js -React.render( +ReactDOM.render( - {() => ...} + ... , targetEl ); @@ -57,26 +60,39 @@ Instead, it *returns* a new, connected component class, for you to use. * [`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. -* [`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. +* [`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. * [`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. * [`options`] *(Object)* If specified, further customizes the behavior of the connector. - * [`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`.* + * [`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`.* + * [`withRef = false`] *(Boolean)*: If true, stores a ref to the wrapped component instance and makes it available via `getWrappedInstance()` method. *Defaults to `false`.* #### Returns A React component class that injects state and action creators into your component according to the specified options. +##### Static Properties + +* `WrappedComponent` *(Component)*: The original component class passed to `connect()`. + +##### Static Methods + +All the original static methods of the component are hoisted. + +##### Instance Methods + +###### `getWrappedInstance(): ReactComponent` + +Returns the wrapped component instance. Only available if you pass `{ withRef: true }` as part of the `connect()`’s fourth `options` argument. + #### Remarks * 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)`. * It does not modify the passed React component. It returns a new, connected component, that you should use instead. -* 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). - -* **To use `connect()`, the root component of your app must be wrapped into `{() => ... }` 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. +* 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). #### Examples diff --git a/docs/quick-start.md b/docs/quick-start.md index de0c15797..02be890f4 100644 --- a/docs/quick-start.md +++ b/docs/quick-start.md @@ -126,9 +126,10 @@ This is the most basic usage, but `connect()` supports many other patterns: just 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. -The trick is to wrap the whole view hierarchy into a `{() => ... }` 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 `` without wrapping it into a function. +The trick is to wrap the whole view hierarchy into a `` from React Redux. ```js +import ReactDOM from 'react-dom'; import { Component } from 'react'; import { Provider } from 'react-redux'; @@ -140,9 +141,10 @@ class App extends Component { const targetEl = document.getElementById('root'); -React.render(( +ReactDOM.render( - {() => } - -), targetEl); + + , + targetEl +); ``` diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index c0743e18b..2eca50b4d 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -18,9 +18,10 @@ Root view: ```js Router.run(routes, Router.HistoryLocation, (Handler, routerState) => { // note "routerState" here - React.render( + ReactDOM.render( - {() => } // note "routerState" here + {/* note "routerState" here */} + , document.getElementById('root') ); @@ -41,13 +42,13 @@ You can also upgrade to React Router 1.0 which shouldn’t have this problem. (L ### My views aren’t updating when something changes outside of Redux -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. +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. >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). 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. -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()`: +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()`: ``` function mapStateToProps(state) { @@ -67,10 +68,8 @@ If you have context issues, 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. 2. Make sure you didn’t forget to wrap your root component in [``](#provider-store). -3. If you use React Router, something like `{() => router}` won’t work. Due to the way context works in React 0.13, it’s important that the `` children are *created* inside that function. Just referencing an outside variable doesn’t do the trick. Instead of `{() => router}`, write `{() => createRouter()}` where `createRouter()` is a function that actually *creates* (and returns) the router. +3. Make sure you’re running the latest versions of React and React Redux. ### 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 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. - -If you’re using React Native, make sure you’re importing `react-redux/native` both for `` and any `connect()` call. Importing from `react-redux` will not work on React Native.