Skip to content

Commit 5088345

Browse files
wgao19markerikson
authored andcommitted
Add connect() API doc (#1140)
* Add `connect()` doc * Move remaining api.md to `createProvider()` and `connectAdvanced()` docs, repectively * Hide duplicated title for Provider.md * Update doc site configurations for connect doc * Fix `siteConfig.js` * Fix some links. * More betterer link fix... * Clean up connect docs * Remove createProvider page * Clean up connectAdvanced * Include docs in formatting * Specify format spacing * Format docs content * Remove stray semi
1 parent 4b03b70 commit 5088345

17 files changed

+974
-755
lines changed

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"semi": false,
3-
"singleQuote": true
3+
"singleQuote": true,
4+
"tabWidth": 2
45
}

docs/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
- [Basic Tutorial](./introduction/basic-tutorial.md)
66
- Using React Redux
77
- [Connect: Extracting Data with `mapStateToProps`](./using-react-redux/connect-extracting-data-with-mapStateToProps.md)
8-
- [API](api.md#api)
9-
- [`<Provider store>`](api.md#provider-store)
10-
- [`connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])`](api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options)
11-
- [`connectAdvanced(selectorFactory, [connectOptions])`](api.md#connectadvancedselectorfactory-connectoptions)
8+
- API
9+
- [`<Provider store>`](./api/Provider.md)
10+
- [`connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])`](./api/connect.md)
11+
- [`connectAdvanced(selectorFactory, [connectOptions])`](./api/connect-advanced.md)
1212
- [Troubleshooting](troubleshooting.md#troubleshooting)

docs/api.md

Lines changed: 0 additions & 491 deletions
This file was deleted.

docs/api/Provider.md

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
id: provider
33
title: Provider
44
sidebar_label: Provider
5+
hide_title: true
56
---
67

78
# `<Provider />`
@@ -18,62 +19,60 @@ Note: If you really need to, you can manually pass `store` as a prop to a connec
1819

1920
### Props
2021

21-
`store` (Redux Store)
22+
`store` ([Redux Store](https://redux.js.org/api/store))
2223
The single Redux `store` in your application.
2324

2425
`children` (ReactElement)
2526
The root of your component hierarchy.
2627

27-
2828
### Example Usage
2929

3030
In the example below, the `<App />` component is our root-level component. This means it’s at the very top of our component hierarchy.
3131

3232
**Vanilla React Example**
3333

3434
```jsx
35-
import React from 'react';
36-
import ReactDOM from 'react-dom';
37-
import { Provider } from 'react-redux';
38-
39-
import { App } from './App';
40-
import createStore from './createReduxStore';
35+
import React from 'react'
36+
import ReactDOM from 'react-dom'
37+
import { Provider } from 'react-redux'
4138

42-
const store = createStore();
39+
import { App } from './App'
40+
import createStore from './createReduxStore'
4341

44-
ReactDOM.render(
45-
<Provider store={store}>
46-
<App />
47-
</Provider>,
48-
document.getElementById('root')
49-
)
50-
```
42+
const store = createStore()
5143

44+
ReactDOM.render(
45+
<Provider store={store}>
46+
<App />
47+
</Provider>,
48+
document.getElementById('root')
49+
)
50+
```
5251

5352
**Usage with React Router**
5453

5554
```jsx
56-
import React from 'react';
57-
import ReactDOM from 'react-dom';
58-
import { Provider } from 'react-redux';
59-
import { Router, Route } from 'react-router-dom';
60-
61-
import { App } from './App';
62-
import { Foo } from './Foo';
63-
import { Bar } from './Bar';
64-
import createStore from './createReduxStore';
65-
66-
const store = createStore();
67-
68-
ReactDOM.render(
69-
<Provider store={store}>
70-
<Router history={history}>
71-
<Route path="/" component={App}>
72-
<Route path="foo" component={Foo}/>
73-
<Route path="bar" component={Bar}/>
74-
</Route>
75-
</Router>
76-
</Provider>,
77-
document.getElementById('root')
78-
)
79-
```
55+
import React from 'react'
56+
import ReactDOM from 'react-dom'
57+
import { Provider } from 'react-redux'
58+
import { Router, Route } from 'react-router-dom'
59+
60+
import { App } from './App'
61+
import { Foo } from './Foo'
62+
import { Bar } from './Bar'
63+
import createStore from './createReduxStore'
64+
65+
const store = createStore()
66+
67+
ReactDOM.render(
68+
<Provider store={store}>
69+
<Router history={history}>
70+
<Route path="/" component={App}>
71+
<Route path="foo" component={Foo} />
72+
<Route path="bar" component={Bar} />
73+
</Route>
74+
</Router>
75+
</Provider>,
76+
document.getElementById('root')
77+
)
78+
```

docs/api/connect-advanced.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
id: connect-advanced
3+
title: connectAdvanced
4+
sidebar_label: connectAdvanced()
5+
hide_title: true
6+
---
7+
8+
# `connectAdvanced()`
9+
10+
```js
11+
connectAdvanced(selectorFactory, connectOptions?)
12+
```
13+
14+
Connects a React component to a Redux store. It is the base for `connect()` but is less opinionated about how to combine `state`, `props`, and `dispatch` into your final props. It makes no assumptions about defaults or memoization of results, leaving those responsibilities to the caller.
15+
16+
It does not modify the component class passed to it; instead, it _returns_ a new, connected component class for you to use.
17+
18+
Most applications will not need to use this, as the default behavior in `connect` is intended to work for most use cases.
19+
20+
> Note: `connectAdvanced` was added in version 5.0, and `connect` was reimplemented as a specific set of parameters to `connectAdvanced`.
21+
22+
## Arguments
23+
24+
- `selectorFactory(dispatch, factoryOptions): selector(state, ownProps): props` \(_Function_): Initializes a selector function (during each instance's constructor). That selector function is called any time the connector component needs to compute new props, as a result of a store state change or receiving new props. The result of `selector` is expected to be a plain object, which is passed as the props to the wrapped component. If a consecutive call to `selector` returns the same object (`===`) as its previous call, the component will not be re-rendered. It's the responsibility of `selector` to return that previous object when appropriate.
25+
26+
- [`connectOptions`] _(Object)_ If specified, further customizes the behavior of the connector.
27+
28+
- [`getDisplayName`] _(Function)_: computes the connector component's displayName property relative to that of the wrapped component. Usually overridden by wrapper functions. Default value: `name => 'ConnectAdvanced('+name+')'`
29+
30+
- [`methodName`] _(String)_: shown in error messages. Usually overridden by wrapper functions. Default value: `'connectAdvanced'`
31+
32+
- [`renderCountProp`] _(String)_: if defined, a property named this value will be added to the props passed to the wrapped component. Its value will be the number of times the component has been rendered, which can be useful for tracking down unnecessary re-renders. Default value: `undefined`
33+
34+
- [`shouldHandleStateChanges`] _(Boolean)_: controls whether the connector component subscribes to redux store state changes. If set to false, it will only re-render when parent component re-renders. Default value: `true`
35+
36+
- [`forwardRef`] _(Boolean)_: If true, adding a ref to the connected wrapper component will actually return the instance of the wrapped component.
37+
38+
- Additionally, any extra options passed via `connectOptions` will be passed through to your `selectorFactory` in the `factoryOptions` argument.
39+
40+
<a id="connectAdvanced-returns"></a>
41+
42+
## Returns
43+
44+
A higher-order React component class that builds props from the store state and passes them to the wrapped component. A higher-order component is a function which accepts a component argument and returns a new component.
45+
46+
### Static Properties
47+
48+
- `WrappedComponent` _(Component)_: The original component class passed to `connectAdvanced(...)(Component)`.
49+
50+
### Static Methods
51+
52+
All the original static methods of the component are hoisted.
53+
54+
## Remarks
55+
56+
- Since `connectAdvanced` returns a higher-order component, it needs to be invoked two times. The first time with its arguments as described above, and a second time, with the component: `connectAdvanced(selectorFactory)(MyComponent)`.
57+
58+
- `connectAdvanced` does not modify the passed React component. It returns a new, connected component, that you should use instead.
59+
60+
<a id="connectAdvanced-examples"></a>
61+
62+
### Examples
63+
64+
### Inject `todos` of a specific user depending on props, and inject `props.userId` into the action
65+
66+
```js
67+
import * as actionCreators from './actionCreators'
68+
import { bindActionCreators } from 'redux'
69+
70+
function selectorFactory(dispatch) {
71+
let ownProps = {}
72+
let result = {}
73+
74+
const actions = bindActionCreators(actionCreators, dispatch)
75+
const addTodo = text => actions.addTodo(ownProps.userId, text)
76+
77+
return (nextState, nextOwnProps) => {
78+
const todos = nextState.todos[nextOwnProps.userId]
79+
const nextResult = { ...nextOwnProps, todos, addTodo }
80+
ownProps = nextOwnProps
81+
if (!shallowEqual(result, nextResult)) result = nextResult
82+
return result
83+
}
84+
}
85+
export default connectAdvanced(selectorFactory)(TodoApp)
86+
```

0 commit comments

Comments
 (0)