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
Copy file name to clipboardExpand all lines: docs/api/Provider.md
+3-4Lines changed: 3 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,11 @@ id: provider
3
3
title: Provider
4
4
sidebar_label: Provider
5
5
hide_title: true
6
+
description: 'API > Provider: providing the Redux store to your React app'
6
7
---
7
8
9
+
10
+
8
11
# `Provider`
9
12
10
13
## Overview
@@ -30,13 +33,10 @@ You may provide a context instance. If you do so, you will need to provide the s
30
33
>
31
34
> 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.
32
35
33
-
34
-
35
36
### Example Usage
36
37
37
38
In the example below, the `<App />` component is our root-level component. This means it’s at the very top of our component hierarchy.
React's `unstable_batchedUpdates()` API allows any React updates in an event loop tick to be batched together into a single render pass. React already uses this internally for its own event handler callbacks. This API is actually part of the renderer packages like ReactDOM and React Native, not the React core itself.
Copy file name to clipboardExpand all lines: docs/api/connect-advanced.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,11 @@ id: connect-advanced
3
3
title: connectAdvanced
4
4
sidebar_label: connectAdvanced()
5
5
hide_title: true
6
+
description: 'API > connectAdvanced: customizing connect for advanced behavior'
6
7
---
7
8
9
+
10
+
8
11
# `connectAdvanced()`
9
12
10
13
```js
@@ -19,11 +22,10 @@ Most applications will not need to use this, as the default behavior in `connect
19
22
20
23
:::info
21
24
22
-
`connectAdvanced` was added in version 5.0, and `connect` was reimplemented as a specific set of parameters to `connectAdvanced`. However, [**`connectAdvanced` is now deprecated**](https://github.com/reduxjs/react-redux/issues/1236) and will eventually be removed in a future major version of React Redux.
25
+
`connectAdvanced` was added in version 5.0, and `connect` was reimplemented as a specific set of parameters to `connectAdvanced`. However, [**`connectAdvanced` is now deprecated**](https://github.com/reduxjs/react-redux/issues/1236) and will eventually be removed in a future major version of React Redux.
23
26
24
27
:::
25
28
26
-
27
29
## Arguments
28
30
29
31
- `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.
While the `connect` API has stayed almost entirely API-compatible between all of our major versions, there have been some small changes in options and behavior from version to version.
Copy file name to clipboardExpand all lines: docs/api/hooks.md
+16-13Lines changed: 16 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,13 +3,16 @@ id: hooks
3
3
title: Hooks
4
4
sidebar_label: Hooks
5
5
hide_title: true
6
+
description: 'API > Hooks: the `useSelector` and `useDispatch` hooks`'
6
7
---
7
8
9
+
10
+
8
11
# Hooks
9
12
10
-
React's new ["hooks" APIs](https://reactjs.org/docs/hooks-intro.html) give function components the ability to use local component state, execute side effects, and more. React also lets us write [custom hooks](https://reactjs.org/docs/hooks-custom.html), which let us extract reusable hooks to add our own behavior on top of React's built-in hooks.
13
+
React's new ["hooks" APIs](https://reactjs.org/docs/hooks-intro.html) give function components the ability to use local component state, execute side effects, and more. React also lets us write [custom hooks](https://reactjs.org/docs/hooks-custom.html), which let us extract reusable hooks to add our own behavior on top of React's built-in hooks.
11
14
12
-
React Redux includes its own custom hook APIs, which allow your React components to subscribe to the Redux store and dispatch actions.
15
+
React Redux includes its own custom hook APIs, which allow your React components to subscribe to the Redux store and dispatch actions.
13
16
14
17
:::tip
15
18
@@ -107,7 +110,7 @@ import React from 'react'
107
110
import { useSelector } from'react-redux'
108
111
109
112
exportconstCounterComponent= () => {
110
-
constcounter=useSelector(state=>state.counter)
113
+
constcounter=useSelector((state)=>state.counter)
111
114
return<div>{counter}</div>
112
115
}
113
116
```
@@ -118,8 +121,8 @@ Using props via closure to determine what to extract:
[React Redux](https://github.com/reduxjs/react-redux) is the official [React](https://reactjs.org/) UI bindings layer for [Redux](https://redux.js.org/). It lets your React components read data from a Redux store, and dispatch actions to the store to update state.
@@ -15,10 +21,14 @@ React Redux 7.1+ requires **React 16.8.3 or later**, in order to make use of Rea
15
21
16
22
### Using Create React App
17
23
18
-
The recommended way to start new apps with React Redux is by using the [official Redux+JS template](https://github.com/reduxjs/cra-template-redux) for [Create React App](https://github.com/facebook/create-react-app), which takes advantage of [Redux Toolkit](https://redux-toolkit.js.org/).
24
+
The recommended way to start new apps with React and Redux is by using the [official Redux+JS template](https://github.com/reduxjs/cra-template-redux)or [Redux+TS template](https://github.com/reduxjs/cra-template-redux-typescript)for [Create React App](https://github.com/facebook/create-react-app), which takes advantage of **[Redux Toolkit](https://redux-toolkit.js.org/)** and React Redux's integration with React components.
You'll also need to [install Redux](https://redux.js.org/introduction/installation) and [set up a Redux store](https://redux.js.org/recipes/configuring-your-store/) in your app.
37
47
38
-
If you are using TypeScript, the React Redux types are maintained separately in DefinitelyTyped. You'll need to install those as well:
48
+
If you are using TypeScript, the React Redux types are maintained separately in DefinitelyTyped, but included as a dependency of the `react-redux` package, so they should be installed automatically. If you still need to install them manually, run:
39
49
40
50
```bash
41
51
npm install @types/react-redux
42
52
```
43
53
44
-
The code used for this example is based on the [official Redux template](https://github.com/reduxjs/cra-template-redux). Additionally, the same code template for TypeScript can be found [here](https://github.com/reduxjs/cra-template-redux-typescript).
54
+
## API Overview
45
55
46
-
## `Provider`
56
+
###`Provider`
47
57
48
58
React Redux includes a `<Provider />` component, which makes the Redux store available to the rest of your app:
49
59
50
-
```js
60
+
```jsx
51
61
importReactfrom'react'
52
62
importReactDOMfrom'react-dom'
53
63
@@ -65,21 +75,21 @@ ReactDOM.render(
65
75
)
66
76
```
67
77
68
-
## Hooks
78
+
###Hooks
69
79
70
80
React Redux provides a pair of custom React hooks that allow your React components to interact with the Redux store.
71
81
72
82
`useSelector` reads a value from the store state and subscribes to updates, while `useDispatch` returns the store's `dispatch` method to let you dispatch actions.
Redux maintainer Mark Erikson appeared on the "Learn with Jason" show to explain how we recommend using Redux today. The show includes a live-coded example app that shows how to use Redux Toolkit and React-Redux hooks with Typescript, as well as the new RTK Query data fetching APIs.
130
+
131
+
See [the "Learn Modern Redux" show notes page](https://www.learnwithjason.dev/let-s-learn-modern-redux) for a transcript and links to the example app source.
132
+
133
+
<LiteYouTubeEmbed
134
+
id="9zySeP5vH9c"
135
+
title="Learn Modern Redux - Redux Toolkit, React-Redux Hooks, and RTK Query"
136
+
/>
137
+
115
138
## Help and Discussion
116
139
117
140
The **[#redux channel](https://discord.gg/0ZcbPKXt5bZ6au5t)** of the **[Reactiflux Discord community](http://www.reactiflux.com)** is our official resource for all questions related to learning and using Redux. Reactiflux is a great place to hang out, ask questions, and learn - come join us!
Copy file name to clipboardExpand all lines: docs/introduction/why-use-react-redux.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,11 @@ id: why-use-react-redux
3
3
title: Why Use React Redux?
4
4
hide_title: true
5
5
sidebar_label: Why Use React Redux?
6
+
description: 'Introduction > Why Use React Redux: benefits of using React Redux in a React app'
6
7
---
7
8
9
+
10
+
8
11
# Why Use React Redux?
9
12
10
13
Redux itself is a standalone library that can be used with any UI layer or framework, including React, Angular, Vue, Ember, and vanilla JS. Although Redux and React are commonly used together, they are independent of each other.
Copy file name to clipboardExpand all lines: docs/troubleshooting.md
+3-4Lines changed: 3 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,21 +5,20 @@ sidebar_label: Troubleshooting
5
5
hide_title: true
6
6
---
7
7
8
+
9
+
8
10
## Troubleshooting
9
11
10
12
The **[#redux channel](https://discord.gg/0ZcbPKXt5bZ6au5t)** of the **[Reactiflux Discord community](http://www.reactiflux.com)** is our official resource for all questions related to learning and using Redux. Reactiflux is a great place to hang out, ask questions, and learn - come join us!
11
13
12
14
You can also ask questions on [Stack Overflow](https://stackoverflow.com) using the **[#redux tag](https://stackoverflow.com/questions/tagged/redux)**.
13
15
14
-
15
16
### My views aren’t updating!
16
17
17
18
In short,
18
19
19
20
- Reducers should never mutate state, they must return new objects, or React Redux won’t see the updates.
20
-
- Make sure you are actually _dispatching_ actions. For example, if you have an action creator like `addTodo`, just calling the imported `addTodo()` function by itself won't do anything because it just _returns_ an action, but does not _dispatch_ it. You either need to call `dispatch(addTodo())` (if using the hooks API) or `props.addTodo()` (if using `connect` + `mapDispatch`).
21
-
22
-
21
+
- Make sure you are actually _dispatching_ actions. For example, if you have an action creator like `addTodo`, just calling the imported `addTodo()` function by itself won't do anything because it just _returns_ an action, but does not _dispatch_ it. You either need to call `dispatch(addTodo())` (if using the hooks API) or `props.addTodo()` (if using `connect` + `mapDispatch`).
23
22
24
23
### Could not find "store" in either the context or props
Copy file name to clipboardExpand all lines: docs/tutorials/connect.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,11 @@ id: connect
3
3
title: 'Tutorial: Connect API'
4
4
hide_title: true
5
5
sidebar_label: 'Tutorial: Connect API'
6
+
description: 'Tutorials > Connect API: how to use the legacy connect API'
6
7
---
7
8
9
+
10
+
8
11
# Tutorial: Using the `connect` API
9
12
10
13
:::tip
@@ -13,6 +16,8 @@ We now recommend using [the React-Redux hooks API as the default](../api/hooks.m
13
16
14
17
This tutorial also shows some older practices we no longer recommend, like separating Redux logic into folders by type. We've kept this tutorial as-is for completeness, but recommend reading through [the "Redux Essentials" tutorial](https://redux.js.org/tutorials/essentials/part-1-overview-concepts) and the [Redux Style Guide](https://redux.js.org/style-guide/style-guide) in the Redux docs for our current best practices.
15
18
19
+
We're working on a new tutorial that will introduce the hooks APIs. Until then, we suggest reading [**Redux Fundamentals, Part 5: UI and React**](https://redux.js.org/tutorials/fundamentals/part-5-ui-react) for a hooks tutorial.
20
+
16
21
:::
17
22
18
23
To see how to use React Redux in practice, we’ll show a step-by-step example by creating a todo list app.
0 commit comments