Skip to content

Commit 69f5014

Browse files
committed
Cast v6.x versioned docs
1 parent a726579 commit 69f5014

12 files changed

+2352
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
id: version-6.x-provider
3+
title: Provider
4+
sidebar_label: Provider
5+
hide_title: true
6+
original_id: provider
7+
---
8+
9+
# `Provider`
10+
11+
## Overview
12+
13+
The `<Provider />` makes the Redux `store` available to any nested components that have been wrapped in the `connect()` function.
14+
15+
Since any React component in a React Redux app can be connected, most applications will render a `<Provider>` at the top level, with the entire app’s component tree inside of it.
16+
17+
Normally, you can’t use a connected component unless it is nested inside of a `<Provider>`.
18+
19+
### Props
20+
21+
`store` ([Redux Store](https://redux.js.org/api/store))
22+
The single Redux `store` in your application.
23+
24+
`children` (ReactElement)
25+
The root of your component hierarchy.
26+
27+
`context`
28+
You may provide a context instance. If you do so, you will need to provide the same context instance to all of your connected components as well. Failure to provide the correct context results in runtime error:
29+
30+
> Invariant Violation
31+
>
32+
> Could not find "store" in the context of "Connect(MyComponent)". Either wrap the root component in a `<Provider>`, or pass a custom React context provider to `<Provider>` and the corresponding React context consumer to Connect(Todo) in connect options.
33+
34+
**Note:** You do not need to provide custom context in order to access the store.
35+
React Redux exports the context instance it uses by default so that you can access the store by:
36+
37+
```js
38+
import { ReactReduxContext } from 'react-redux'
39+
40+
// in your connected component
41+
render() {
42+
return (
43+
<ReactReduxContext.Consumer>
44+
{({ store }) => {
45+
// do something with the store here
46+
}}
47+
</ReactReduxContext.Consumer>
48+
)
49+
}
50+
```
51+
52+
### Example Usage
53+
54+
In the example below, the `<App />` component is our root-level component. This means it’s at the very top of our component hierarchy.
55+
56+
**Vanilla React Example**
57+
58+
```jsx
59+
import React from 'react'
60+
import ReactDOM from 'react-dom'
61+
import { Provider } from 'react-redux'
62+
63+
import { App } from './App'
64+
import createStore from './createReduxStore'
65+
66+
const store = createStore()
67+
68+
ReactDOM.render(
69+
<Provider store={store}>
70+
<App />
71+
</Provider>,
72+
document.getElementById('root')
73+
)
74+
```
75+
76+
**Usage with React Router**
77+
78+
```jsx
79+
import React from 'react'
80+
import ReactDOM from 'react-dom'
81+
import { Provider } from 'react-redux'
82+
import { Router, Route } from 'react-router-dom'
83+
84+
import { App } from './App'
85+
import { Foo } from './Foo'
86+
import { Bar } from './Bar'
87+
import createStore from './createReduxStore'
88+
89+
const store = createStore()
90+
91+
ReactDOM.render(
92+
<Provider store={store}>
93+
<Router history={history}>
94+
<Route exact path="/" component={App} />
95+
<Route path="/foo" component={Foo} />
96+
<Route path="/bar" component={Bar} />
97+
</Router>
98+
</Provider>,
99+
document.getElementById('root')
100+
)
101+
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
id: version-6.x-connect-advanced
3+
title: connectAdvanced
4+
sidebar_label: connectAdvanced()
5+
hide_title: true
6+
original_id: connect-advanced
7+
---
8+
9+
# `connectAdvanced()`
10+
11+
```js
12+
connectAdvanced(selectorFactory, connectOptions?)
13+
```
14+
15+
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.
16+
17+
It does not modify the component class passed to it; instead, it _returns_ a new, connected component class for you to use.
18+
19+
Most applications will not need to use this, as the default behavior in `connect` is intended to work for most use cases.
20+
21+
> Note: `connectAdvanced` was added in version 5.0, and `connect` was reimplemented as a specific set of parameters to `connectAdvanced`.
22+
23+
## Arguments
24+
25+
- `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.
26+
27+
- [`connectOptions`] _(Object)_ If specified, further customizes the behavior of the connector.
28+
29+
- [`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+')'`
30+
31+
- [`methodName`] _(String)_: shown in error messages. Usually overridden by wrapper functions. Default value: `'connectAdvanced'`
32+
33+
- [`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`
34+
35+
- [`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`
36+
37+
- [`forwardRef`] _(Boolean)_: If true, adding a ref to the connected wrapper component will actually return the instance of the wrapped component.
38+
39+
- Additionally, any extra options passed via `connectOptions` will be passed through to your `selectorFactory` in the `factoryOptions` argument.
40+
41+
<a id="connectAdvanced-returns"></a>
42+
43+
## Returns
44+
45+
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.
46+
47+
### Static Properties
48+
49+
- `WrappedComponent` _(Component)_: The original component class passed to `connectAdvanced(...)(Component)`.
50+
51+
### Static Methods
52+
53+
All the original static methods of the component are hoisted.
54+
55+
## Remarks
56+
57+
- 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)`.
58+
59+
- `connectAdvanced` does not modify the passed React component. It returns a new, connected component, that you should use instead.
60+
61+
<a id="connectAdvanced-examples"></a>
62+
63+
### Examples
64+
65+
### Inject `todos` of a specific user depending on props, and inject `props.userId` into the action
66+
67+
```js
68+
import * as actionCreators from './actionCreators'
69+
import { bindActionCreators } from 'redux'
70+
71+
function selectorFactory(dispatch) {
72+
let ownProps = {}
73+
let result = {}
74+
75+
const actions = bindActionCreators(actionCreators, dispatch)
76+
const addTodo = text => actions.addTodo(ownProps.userId, text)
77+
78+
return (nextState, nextOwnProps) => {
79+
const todos = nextState.todos[nextOwnProps.userId]
80+
const nextResult = { ...nextOwnProps, todos, addTodo }
81+
ownProps = nextOwnProps
82+
if (!shallowEqual(result, nextResult)) result = nextResult
83+
return result
84+
}
85+
}
86+
export default connectAdvanced(selectorFactory)(TodoApp)
87+
```

0 commit comments

Comments
 (0)