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
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`.
72
72
73
-
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)).
73
+
The return of the `mapStateToProps` determine whether the connected component will re-render (details [here](../using-react-redux/connect-extracting-data-with-mapStateToProps.md#return-values-determine-if-your-component-re-renders)).
74
74
75
-
For more details on recommended usage of `mapStateToProps`, please refer to [our guide on using `mapStateToProps`](../using-react-redux/connect-mapstate).
75
+
For more details on recommended usage of `mapStateToProps`, please refer to [our guide on using `mapStateToProps`](../using-react-redux/connect-extracting-data-with-mapStateToProps.md).
76
76
77
77
> 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.
Copy file name to clipboardExpand all lines: docs/api/hooks.md
+3-4
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,7 @@ The selector function should be [pure](https://en.wikipedia.org/wiki/Pure_functi
55
55
56
56
:::
57
57
58
-
The selector is approximately equivalent to the [`mapStateToProps` argument to `connect`](../using-react-redux/connect-mapstate) conceptually. The selector will be called with the entire Redux store state as its only argument. The selector will be run whenever the function component renders (unless its reference hasn't changed since a previous render of the component so that a cached result can be returned by the hook without re-running the selector). `useSelector()` will also subscribe to the Redux store, and run your selector whenever an action is dispatched.
58
+
The selector is approximately equivalent to the [`mapStateToProps` argument to `connect`](../using-react-redux/connect-extracting-data-with-mapStateToProps.md) conceptually. The selector will be called with the entire Redux store state as its only argument. The selector will be run whenever the function component renders (unless its reference hasn't changed since a previous render of the component so that a cached result can be returned by the hook without re-running the selector). `useSelector()` will also subscribe to the Redux store, and run your selector whenever an action is dispatched.
59
59
60
60
However, there are some differences between the selectors passed to `useSelector()` and a `mapState` function:
61
61
@@ -190,7 +190,7 @@ export const App = () => {
190
190
}
191
191
```
192
192
193
-
However, when the selector is used in multiple component instances and depends on the component's props, you need to ensure that each component instance gets its own selector instance (see [here](https://github.com/reduxjs/reselect#accessing-react-props-in-selectors) for a more thorough explanation of why this is necessary):
193
+
However, when the selector is used in multiple component instances and depends on the component's props, you need to ensure that each component instance gets its own selector instance (see [here](https://github.com/reduxjs/reselect#q-can-i-share-a-selector-across-multiple-component-instances) for a more thorough explanation of why this is necessary):
194
194
195
195
```jsx
196
196
importReact, { useMemo } from'react'
@@ -290,7 +290,7 @@ However, the React hooks lint rules do not know that `dispatch` should be stable
290
290
should be added to dependency arrays for `useEffect` and `useCallback`. The simplest solution is to do just that:
291
291
292
292
````js
293
-
exportconstTodos()= () => {
293
+
exportconstTodos= () => {
294
294
constdispatch=useDispatch();
295
295
296
296
useEffect(() => {
@@ -481,4 +481,3 @@ export function useShallowEqualSelector(selector) {
481
481
### Additional considerations when using hooks
482
482
483
483
There are some architectural trade offs to take into consideration when deciding whether to use hooks or not. Mark Erikson summarizes these nicely in his two blog posts [Thoughts on React Hooks, Redux, and Separation of Concerns](https://blog.isquaredsoftware.com/2019/07/blogged-answers-thoughts-on-hooks/) and [Hooks, HOCs, and Tradeoffs](https://blog.isquaredsoftware.com/2019/09/presentation-hooks-hocs-tradeoffs/).
Copy file name to clipboardExpand all lines: docs/troubleshooting.md
+3-2
Original file line number
Diff line number
Diff line change
@@ -25,8 +25,9 @@ In short,
25
25
If you have context issues,
26
26
27
27
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.
28
-
2. Make sure you didn’t forget to wrap your root or some other ancestor component in [`<Provider>`](#provider-store).
29
-
3. Make sure you’re running the latest versions of React and React Redux.
28
+
2. Make sure you don't have multiple instances/copies of React Redux in your project.
29
+
3. Make sure you didn’t forget to wrap your root or some other ancestor component in [`<Provider>`](#provider-store).
30
+
4. Make sure you’re running the latest versions of React and React Redux.
30
31
31
32
### 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
Now we can use the React Redux hooks to let React components interact with the Redux store. We can read data from the store with `useSelector`, and dispatch actions using `useDispatch`. Create a `src/features/counter/Counter.js` file with a `<Counter>` component inside, then import that component into `App.js` and render it inside of `<App>`.
Copy file name to clipboardExpand all lines: docs/using-react-redux/accessing-store.md
+4
Original file line number
Diff line number
Diff line change
@@ -75,6 +75,10 @@ The following runtime error occurs when React Redux does not find a store in the
75
75
>
76
76
> 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.
77
77
78
+
### Custom Context and the hooks API
79
+
80
+
To access the custom context via the hooks API, you can create custom hooks via the [hook creator functions](../api/hooks.md#custom-context).
81
+
78
82
## Multiple Stores
79
83
80
84
[Redux was designed to use a single store](https://redux.js.org/api/store#a-note-for-flux-users).
0 commit comments