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
+4-47
Original file line number
Diff line number
Diff line change
@@ -9,11 +9,11 @@ hide_title: true
9
9
10
10
## Overview
11
11
12
-
The `<Provider />` makes the Redux `store` available to any nested components that have been wrapped in the `connect()` function.
12
+
The `<Provider>`component makes the Redux `store` available to any nested components that need to access the Redux store.
13
13
14
-
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.
14
+
Since any React component in a React Redux app can be connected to the store, most applications will render a `<Provider>` at the top level, with the entire app’s component tree inside of it.
15
15
16
-
Normally, you can’t use a connected component unless it is nested inside of a `<Provider>`.
16
+
The [Hooks](./hooks.md) and [`connect`](./connect.md) APIs can then access the provided store instance via React's Context mechanism.
17
17
18
18
### Props
19
19
@@ -30,29 +30,12 @@ You may provide a context instance. If you do so, you will need to provide the s
30
30
>
31
31
> 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
32
33
-
**Note:** You do not need to provide custom context in order to access the store.
34
-
React Redux exports the context instance it uses by default so that you can access the store by:
35
-
36
-
```js
37
-
import { ReactReduxContext } from'react-redux'
38
-
39
-
// in your connected component
40
-
render() {
41
-
return (
42
-
<ReactReduxContext.Consumer>
43
-
{({ store }) => {
44
-
// do something with the store here
45
-
}}
46
-
</ReactReduxContext.Consumer>
47
-
)
48
-
}
49
-
```
33
+
50
34
51
35
### Example Usage
52
36
53
37
In the example below, the `<App />` component is our root-level component. This means it’s at the very top of our component hierarchy.
Copy file name to clipboardexpand all lines: docs/api/batch.md
+2
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,8 @@ hide_title: true
11
11
batch((fn:Function))
12
12
```
13
13
14
+
*added in v7.0.0*
15
+
14
16
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.
15
17
16
18
Since React-Redux needs to work in both ReactDOM and React Native environments, we've taken care of importing this API from the correct renderer at build time for our own use. We also now re-export this function publicly ourselves, renamed to `batch()`. You can use it to ensure that multiple actions dispatched outside of React only result in a single render update, like this:
Copy file name to clipboardexpand all lines: docs/api/connect-advanced.md
+6-1
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,12 @@ It does not modify the component class passed to it; instead, it _returns_ a new
17
17
18
18
Most applications will not need to use this, as the default behavior in `connect` is intended to work for most use cases.
19
19
20
-
> Note: `connectAdvanced` was added in version 5.0, and `connect` was reimplemented as a specific set of parameters to `connectAdvanced`.
20
+
:::info
21
+
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.
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.
585
+
586
+
For details on the legacy 5.x and 6.x versions, please see these archived files in the React Redux repo:
587
+
588
+
- [5.x `connect` API reference](https://github.com/reduxjs/react-redux/blob/v7.2.2/website/versioned_docs/version-5.x/api/connect.md)
589
+
- [6.x `connect` API reference](https://github.com/reduxjs/react-redux/blob/v7.2.2/website/versioned_docs/version-6.x/api/connect.md)
Copy file name to clipboardexpand all lines: docs/api/hooks.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,9 @@ hide_title: true
7
7
8
8
# Hooks
9
9
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.
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.
11
11
12
-
React Redux now offers a set of hook APIs as an alternative to the existing `connect()` Higher Order Component. These APIs allow you to subscribe to the Redux store and dispatch actions, without having to wrap your components in `connect()`.
12
+
React Redux includes its own custom hook APIs, which allow your React components to subscribe to the Redux store and dispatch actions.
[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.
11
+
12
+
## Installation
13
+
14
+
React Redux 7.1+ requires **React 16.8.3 or later**, in order to make use of React Hooks.
15
+
16
+
### Using Create React App
17
+
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/).
19
+
20
+
```sh
21
+
npx create-react-app my-app --template redux
22
+
```
23
+
24
+
### An Existing React App
25
+
26
+
To use React Redux with your React app, install it as a dependency:
27
+
28
+
```bash
29
+
# If you use npm:
30
+
npm install react-redux
31
+
32
+
# Or if you use Yarn:
33
+
yarn add react-redux
34
+
```
35
+
36
+
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
+
38
+
If you are using TypeScript, the React Redux types are maintained separately in DefinitelyTyped. You'll need to install those as well:
39
+
40
+
```bash
41
+
npm install @types/react-redux
42
+
```
43
+
44
+
## `Provider`
45
+
46
+
React Redux includes a `<Provider />` component, which makes the Redux store available to the rest of your app:
47
+
48
+
```js
49
+
importReactfrom'react'
50
+
importReactDOMfrom'react-dom'
51
+
52
+
import { Provider } from'react-redux'
53
+
importstorefrom'./store'
54
+
55
+
importAppfrom'./App'
56
+
57
+
constrootElement=document.getElementById('root')
58
+
ReactDOM.render(
59
+
<Provider store={store}>
60
+
<App />
61
+
</Provider>,
62
+
rootElement
63
+
)
64
+
```
65
+
66
+
## Hooks
67
+
68
+
React Redux provides a pair of custom React hooks that allow your React components to interact with the Redux store.
69
+
70
+
`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.
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!
117
+
118
+
You can also ask questions on [Stack Overflow](https://stackoverflow.com) using the **[#redux tag](https://stackoverflow.com/questions/tagged/redux)**.
Copy file name to clipboardexpand all lines: docs/troubleshooting.md
+3-58
Original file line number
Diff line number
Diff line change
@@ -7,74 +7,19 @@ hide_title: true
7
7
8
8
## Troubleshooting
9
9
10
-
Make sure to check out [Troubleshooting Redux](https://redux.js.org/troubleshooting) first.
10
+
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
11
12
-
### I'm getting the following alert: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.
12
+
You can also ask questions on [Stack Overflow](https://stackoverflow.com) using the **[#redux tag](https://stackoverflow.com/questions/tagged/redux)**.
13
13
14
-
This warning is shown when using react 15.5.\*. Basically, now it's just a warning, but in react16 the application might break. the PropTypes should now be imported from 'prop-types' package, and not from the react package.
15
-
16
-
Update to the latest version of react-redux.
17
14
18
15
### My views aren’t updating!
19
16
20
-
See the link above.
21
17
In short,
22
18
23
19
- Reducers should never mutate state, they must return new objects, or React Redux won’t see the updates.
24
-
- Make sure you either bind action creators with the `mapDispatchToProps` argument to `connect()` or with the `bindActionCreators()` method, or that you manually call `dispatch()`. Just calling your `MyActionCreators.addTodo()` function won’t work because it just _returns_ an action, but does not _dispatch_ it.
25
-
26
-
### My views aren’t updating on route change with React Router 0.13
27
-
28
-
If you’re using React Router 0.13, you might [bump into this problem](https://github.com/reduxjs/react-redux/issues/43). The solution is simple: whenever you use `<RouteHandler>` or the `Handler` provided by `Router.run`, pass the router state to it.
Conveniently, this gives your components access to the router state!
55
-
You can also upgrade to React Router 1.0 which shouldn’t have this problem. (Let us know if it does!)
56
-
57
-
### My views aren’t updating when something changes outside of Redux
58
-
59
-
If your views depend on global state or [React “context”](http://facebook.github.io/react/docs/context.html), you might find that views decorated with `connect()` will fail to update.
60
-
61
-
> This is because `connect()` implements [shouldComponentUpdate](https://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate) by default, assuming that your component will produce the same results given the same props and state. This is a similar concept to React’s [PureRenderMixin](https://facebook.github.io/react/docs/pure-render-mixin.html).
62
-
63
-
The _best_ solution to this is to make sure that your components are pure and pass any external state to them via props. This will ensure that your views do not re-render unless they actually need to re-render and will greatly speed up your application.
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`).
64
21
65
-
If that’s not practical for whatever reason (for example, if you’re using a library that depends heavily on React context), you may pass the `pure: false` option to `connect()`:
This will remove the assumption that `TodoApp` is pure and cause it to update whenever its parent component renders. Note that this will make your application less performant, so only do this if you have no other option.
78
23
79
24
### Could not find "store" in either the context or props
Copy file name to clipboardexpand all lines: docs/tutorials/connect.md
+12-4
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,19 @@
1
1
---
2
-
id: basic-tutorial
3
-
title: Basic Tutorial
2
+
id: connect
3
+
title: "Tutorial: Connect API"
4
4
hide_title: true
5
-
sidebar_label: Basic Tutorial
5
+
sidebar_label:
6
6
---
7
7
8
-
# Basic Tutorial
8
+
# Tutorial: Using the `connect` API
9
+
10
+
:::tip
11
+
12
+
We now recommend using [the React-Redux hooks API as the default](../api/hooks.md). However, the `connect` API still works fine.
13
+
14
+
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
+
16
+
:::
9
17
10
18
To see how to use React Redux in practice, we’ll show a step-by-step example by creating a todo list app.
Copy file name to clipboardexpand all lines: docs/using-react-redux/usage-with-typescript.md
+15-17
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,17 @@
1
1
---
2
-
id: static-typing
3
-
title: Static Typing
2
+
id: usage-with-typescript
3
+
title: Usage with TypeScript
4
4
hide_title: true
5
-
sidebar_label: Static Typing
5
+
sidebar_label: Usage with TypeScript
6
6
---
7
7
8
-
# Static Typing
8
+
# Usage with TypeScript
9
9
10
-
React-Redux is currently written in plain JavaScript. However, it works well with static type systems such as TypeScript and Flow.
10
+
React-Redux itself is currently written in plain JavaScript. However, it works well with static type systems such as TypeScript and Flow.
11
11
12
-
## TypeScript
12
+
React-Redux doesn't ship with its own type definitions. If you are using TypeScript you should install the [`@types/react-redux` type definitions](https://npm.im/@types/react-redux) from NPM. In addition to typing the library functions, the types also export some helpers to make it easier to write typesafe interfaces between your Redux store and your React components.
13
13
14
-
React-Redux doesn't ship with its own type definitions. If you are using Typescript you should install the [`@types/react-redux` type definitions](https://npm.im/@types/react-redux) from npm. In addition to typing the library functions, the types also export some helpers to make it easier to write typesafe interfaces between your Redux store and your React components.
15
-
16
-
### Defining the Root State Type
14
+
## Defining the Root State Type
17
15
18
16
Both `mapState` and `useSelector` depend on declaring the type of the complete Redux store state value. While this type could be written by hand, the easiest way to define it is to have TypeScript infer it based on what your root reducer function returns. This way, the type is automatically updated as the reducer functions are modified.
19
17
@@ -29,7 +27,7 @@ export type RootState = ReturnType<typeof rootReducer>
When writing selector functions for use with `useSelector`, you should explicitly define the type of the `state` parameter. TS should be able to then infer the return type of the selector, which will be reused as the return type of the `useSelector` hook:
35
33
@@ -63,7 +61,7 @@ import { useTypedSelector } from './reducer.ts'
63
61
const isOn =useTypedSelector((state) =>state.isOn)
64
62
```
65
63
66
-
###Typing the `useDispatch` hook
64
+
## Typing the `useDispatch` hook
67
65
68
66
By default, the return value of `useDispatch` is the standard `Dispatch` type defined by the Redux core types, so no declarations are needed:
69
67
@@ -88,9 +86,9 @@ export type AppDispatch = typeof store.dispatch
88
86
exportconst useAppDispatch = () =>useDispatch<AppDispatch>() // Export a hook that can be reused to resolve types
89
87
```
90
88
91
-
###Typing the `connect` higher order component
89
+
## Typing the `connect` higher order component
92
90
93
-
####Manually Typing `connect`
91
+
### Manually Typing `connect`
94
92
95
93
The `connect` higher-order component is somewhat complex to type, because there are 3 sources of props: `mapStateToProps`, `mapDispatchToProps`, and props passed in from the parent component. Here's a full example of what it looks like to do that manually.
However, inferring the type of `mapDispatch` this way will break if it is defined as an object and also refers to thunks.
155
153
156
-
#### Inferring The Connected Props Automatically
154
+
### Inferring The Connected Props Automatically
157
155
158
156
`connect` consists of two functions that are called sequentially. The first function accepts `mapState` and `mapDispatch` as arguments, and returns a second function. The second function accepts the component to be wrapped, and returns a new wrapper component that passes down the props from `mapState` and `mapDispatch`. Normally, both functions are called together, like `connect(mapState, mapDispatch)(MyComponent)`.
159
157
@@ -216,7 +214,7 @@ type PropsFromRedux = ConnectedProps<typeof connector>
216
214
exportdefaultconnector(MyComponent)
217
215
```
218
216
219
-
###Recommendations
217
+
## Recommendations
220
218
221
219
The hooks API is generally simpler to use with static types. **If you're looking for the easiest solution for using static types with React-Redux, use the hooks API.**
222
220
@@ -226,7 +224,7 @@ If you're using `connect`, **we recommend using the `ConnectedProps<T>` approach
226
224
227
225
For additional information, see these additional resources:
228
226
229
-
-[Redux docs: Usage with TypeScript](https://redux.js.org/recipes/usage-with-typescript): Examples of how to declare types for actions, reducers, and the store
230
-
-[Redux Toolkit docs: Advanced Tutorial](https://redux-toolkit.js.org/tutorials/advanced-tutorial): shows how to use RTK and the React-Redux hooks API with TypeScript
227
+
-[Redux docs: Usage with TypeScript](https://redux.js.org/recipes/usage-with-typescript): Examples of how to use Redux Toolkit, the Redux core, and React Redux with TypeScript
228
+
-[Redux Toolkit docs: TypeScript Quick start](https://redux-toolkit.js.org/tutorials/typescript): shows how to use RTK and the React-Redux hooks API with TypeScript
231
229
-[React+TypeScript Cheatsheet](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet): a comprehensive guide to using React with TypeScript
232
230
-[React + Redux in TypeScript Guide](https://github.com/piotrwitek/react-redux-typescript-guide): extensive information on patterns for using React and Redux with TypeScript
0 commit comments