From 7402f728c5948cbf01fb07086dcb1ec66968a044 Mon Sep 17 00:00:00 2001 From: Wei Gao Date: Tue, 9 Apr 2019 21:45:49 +0800 Subject: [PATCH] Update v7 docs to be "current version" (#1222) * v7 docs to current version * Update version number for 7.x --- website/pages/en/versions.js | 2 +- website/siteConfig.js | 2 +- .../versioned_docs/version-7.x/api/connect.md | 628 ++++++++++++++++++ .../version-7.x/introduction/quick-start.md | 83 +++ .../using-react-redux/accessing-store.md | 145 ++++ website/versions.json | 1 + 6 files changed, 859 insertions(+), 2 deletions(-) create mode 100644 website/versioned_docs/version-7.x/api/connect.md create mode 100644 website/versioned_docs/version-7.x/introduction/quick-start.md create mode 100644 website/versioned_docs/version-7.x/using-react-redux/accessing-store.md diff --git a/website/pages/en/versions.js b/website/pages/en/versions.js index d090c2ea3..3c7db98e2 100644 --- a/website/pages/en/versions.js +++ b/website/pages/en/versions.js @@ -19,7 +19,7 @@ const versionToReleaseTags = { '5.x': '5.0.0', '6.x': '6.0.0', - '7.x': '7.0.0-beta.0' + '7.x': '7.0.1' } function Versions() { diff --git a/website/siteConfig.js b/website/siteConfig.js index 5e134978f..7c2130de5 100644 --- a/website/siteConfig.js +++ b/website/siteConfig.js @@ -105,7 +105,7 @@ const siteConfig = { * After that, 7.x will no longer appear in "pre-release" versions and we should remove this line * More info: https://docusaurus.io/docs/en/versioning */ - nextVersion: "7.x", + // nextVersion: "7.x", gaTrackingId : "UA-130598673-2", }; diff --git a/website/versioned_docs/version-7.x/api/connect.md b/website/versioned_docs/version-7.x/api/connect.md new file mode 100644 index 000000000..64cc3341a --- /dev/null +++ b/website/versioned_docs/version-7.x/api/connect.md @@ -0,0 +1,628 @@ +--- +id: version-7.x-connect +title: Connect +sidebar_label: connect() +hide_title: true +original_id: connect +--- + +# `connect()` + +## Overview + +The `connect()` function connects a React component to a Redux store. + +It provides its connected component with the pieces of the data it needs from the store, and the functions it can use to dispatch actions to the store. + +It does not modify the component class passed to it; instead, it returns a new, connected component class that wraps the component you passed in. + +```js +function connect(mapStateToProps?, mapDispatchToProps?, mergeProps?, options?) +``` + +The `mapStateToProps` and `mapDispatchToProps` deals with your Redux store’s `state` and `dispatch`, respectively. `state` and `dispatch` will be supplied to your `mapStateToProps` or `mapDispatchToProps` functions as the first argument. + +The returns of `mapStateToProps` and `mapDispatchToProps` are referred to internally as `stateProps` and `dispatchProps`, respectively. They will be supplied to `mergeProps`, if defined, as the first and the second argument, where the third argument will be `ownProps`. The combined result, commonly referred to as `mergedProps`, will then be supplied to your connected component. + +## `connect()` Parameters + +`connect` accepts four different parameters, all optional. By convention, they are called: + +1. `mapStateToProps?: Function` +2. `mapDispatchToProps?: Function | Object` +3. `mergeProps?: Function` +4. `options?: Object` + +### `mapStateToProps?: (state, ownProps?) => Object` + +If a `mapStateToProps` function is specified, the new wrapper component will subscribe to Redux store updates. This means that any time the store is updated, `mapStateToProps` will be called. The results of `mapStateToProps` must be a plain object, which will be merged into the wrapped component’s props. If you don't want to subscribe to store updates, pass `null` or `undefined` in place of `mapStateToProps`. + +#### Parameters + +1. `state: Object` +2. `ownProps?: Object` + +A `mapStateToProps` function takes a maximum of two parameters. The number of declared function parameters (a.k.a. arity) affects when it will be called. This also determines whether the function will receive ownProps. See notes [here](#the-arity-of-maptoprops-functions). + +##### `state` + +If your `mapStateToProps` function is declared as taking one parameter, it will be called whenever the store state changes, and given the store state as the only parameter. + +```js +const mapStateToProps = state => ({ todos: state.todos }) +``` + +##### `ownProps` + +If your `mapStateToProps` function is declared as taking two parameters, it will be called whenever the store state changes _or_ when the wrapper component receives new props (based on shallow equality comparisons). It will be given the store state as the first parameter, and the wrapper component's props as the second parameter. + +The second parameter is normally referred to as `ownProps` by convention. + +```js +const mapStateToProps = (state, ownProps) => ({ + todo: state.todos[ownProps.id] +}) +``` + +#### Returns + +Your `mapStateToProps` functions are expected to return an object. This object, normally referred to as `stateProps`, will be merged as props to your connected component. If you define `mergeProps`, it will be supplied as the first parameter to `mergeProps`. + +The return of the `mapStateToProps` determine whether the connected component will re-render (details [here](../using-react-redux/connect-mapstate#return-values-determine-if-your-component-re-renders)). + +For more details on recommended usage of `mapStateToProps`, please refer to [our guide on using `mapStateToProps`](../using-react-redux/connect-mapstate). + +> You may define `mapStateToProps` and `mapDispatchToProps` as a factory function, i.e., you return a function instead of an object. In this case your returned function will be treated as the real `mapStateToProps` or `mapDispatchToProps`, and be called in subsequent calls. You may see notes on [Factory Functions](#factory-functions) or our guide on performance optimizations. + +### `mapDispatchToProps?: Object | (dispatch, ownProps?) => Object` + +Conventionally called `mapDispatchToProps`, this second parameter to `connect()` may either be an object, a function, or not supplied. + +Your component will receive `dispatch` by default, i.e., when you do not supply a second parameter to `connect()`: + +```js +// do not pass `mapDispatchToProps` +connect()(MyComponent) +connect(mapState)(MyComponent) +connect( + mapState, + null, + mergeProps, + options +)(MyComponent) +``` + +If you define a `mapDispatchToProps` as a function, it will be called with a maximum of two parameters. + +#### Parameters + +1. `dispatch: Function` +2. `ownProps?: Object` + +##### `dispatch` + +If your `mapDispatchToProps` is declared as a function taking one parameter, it will be given the `dispatch` of your `store`. + +```js +const mapDispatchToProps = dispatch => { + return { + // dispatching plain actions + increment: () => dispatch({ type: 'INCREMENT' }), + decrement: () => dispatch({ type: 'DECREMENT' }), + reset: () => dispatch({ type: 'RESET' }) + } +} +``` + +##### `ownProps` + +If your `mapDispatchToProps` function is declared as taking two parameters, it will be called with `dispatch` as the first parameter and the props passed to the wrapper component as the second parameter, and will be re-invoked whenever the connected component receives new props. + +The second parameter is normally referred to as `ownProps` by convention. + +```js +// binds on component re-rendering +;