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
title: Connect: Dispatching Actions with mapDispatchToProps
4
4
hide_title: true
5
-
sidebar_label: Connect: Dispatching Actions with mapStateToProps
5
+
sidebar_label: Connect: Dispatching Actions with mapDispatchToProps
6
6
---
7
7
8
8
# Connect: Dispatching Actions with `mapDispatchToProps`
@@ -18,9 +18,9 @@ React-Redux gives you two ways to let components dispatch actions:
18
18
- By default, a connected component receives `props.dispatch` and can dispatch actions itself.
19
19
-`connect` can accept an argument called `mapDispatchToProps`, which lets you create functions that dispatch when called, and pass those functions as props to your component.
20
20
21
-
The `mapDispatchToProps` functions are normally referred to as `mapDispatch` for short, and of course the actual variable name doesn’t matter in real code.
21
+
The `mapDispatchToProps` functions are normally referred to as `mapDispatch` for short, but the actual variable name used can be whatever you want.
22
22
23
-
## With or Without `mapDispatchToProps`
23
+
## Approaches for Dispatching
24
24
25
25
### Default: `dispatch` as a Prop
26
26
@@ -53,23 +53,22 @@ function Counter({ count, dispatch }) {
53
53
}
54
54
```
55
55
56
-
### Providing A `mapDispatchToProps`
56
+
### Providing A `mapDispatchToProps` Parameter
57
57
58
58
Providing a `mapDispatchToProps` allows you to specify which actions your component might need to dispatch. It lets you provide action dispatching functions as props. Therefore, instead of calling `props.dispatch(() => increment())`, you may call `props.increment()` directly. There are a few reasons why you might want to do that.
59
59
60
60
#### More Declarative
61
61
62
62
First, encapsulating the dispatch logic into function makes the implementation more declarative.
63
-
Dispatching an action and let the Redux store handle the data flow is _how to_ implement the feature, it is not _what_ it does.
64
-
Therefore, instead of saying:
63
+
Dispatching an action and letting the Redux store handle the data flow is _how to_ implement the behavior, rather than _what_ it does.
64
+
65
+
A good example would be dispatching an action when a button is clicked. Connecting the button directly probably doesn't make sense conceptually, and neither does having the button reference `dispatch`.
@@ -78,7 +77,20 @@ Therefore, **if you define your own `mapDispatchToProps`, the connected componen
78
77
79
78
#### Pass Down Action Dispatching Logic to ( Unconnected ) Child Components
80
79
81
-
Furthermore, you also gain the freedom of passing down the action dispatching functions to child ( likely unconnected ) components. Now, both the connected component and its descendant need not be aware of `dispatch`, while performing their communications with the Redux store.
80
+
In addition, you also gain the ability to pass down the action dispatching functions to child ( likely unconnected ) components.
81
+
This allows more components to dispatch actions, while keeping them "unaware" of Redux.
82
+
83
+
```jsx
84
+
// pass down toggleTodo to child component
85
+
// making Todo able to dispatch the toggleTodo action
86
+
constTodoList= ({ todos, toggleTodo }) => (
87
+
<div>
88
+
{todos.map(todo=> (
89
+
<Todo todo={todo} onClick={toggleTodo} />
90
+
))}
91
+
</div>
92
+
);
93
+
```
82
94
83
95
This is what React-Redux’s `connect` does — it encapsulates the logic of talking to the Redux store and lets you not worry about it. And this is what you should totally make full use of in your implementation.
84
96
@@ -87,9 +99,9 @@ This is what React-Redux’s `connect` does — it encapsulates the logic of tal
87
99
The `mapDispatchToProps` parameter can be of two forms. While the function form allows more customization, the object form is easy to use.
88
100
89
101
-**Function form**: Allows more customization, gains access to `dispatch` and optionally `ownProps`
90
-
-**Object short hand form**: More declarative and easier to use
102
+
-**Object shorthand form**: More declarative and easier to use
91
103
92
-
⭐ We recommend using the object form of `mapDispatchToProps` unless you specifically need to customize dispatching behavior in some way.
104
+
> ⭐ **Note:** We recommend using the object form of `mapDispatchToProps` unless you specifically need to customize dispatching behavior in some way.
93
105
94
106
## Defining `mapDispatchToProps` As A Function
95
107
@@ -104,7 +116,8 @@ You may use this chance to write customized functions to be called by your conne
104
116
105
117
**`dispatch`**
106
118
107
-
The `mapDispatchToProps` function will be called with `dispatch` as the argument. You may use this param to wrap around the actions you wish to dispatch, or the action creators that return the actions.
119
+
The `mapDispatchToProps` function will be called with `dispatch` as the first argument.
120
+
You will normally make use of this by returning new functions that call `dispatch()` inside themselves, and either pass in a plain action object directly or pass in the result of an action creator.
Your `mapDispatchToProps` function should return a plain object:
154
167
155
-
- Each field in the object should be a function, calling which should automatically dispatch the action
168
+
- Each field in the object will become a separate prop for your own component, and the value should normally be a function that dispatches an action when called.
156
169
- If you use action creators ( as oppose to plain object actions ) inside `dispatch`, it is a convention to simply name the field key the same name as the action creator:
157
170
158
171
```js
@@ -198,6 +211,8 @@ Wrapping these functions by hand is tedious, so Redux provides a function to sim
198
211
1. A **`function`** (an action creator) or an **`object`** (each field an action creator)
199
212
2.`dispatch`
200
213
214
+
The wrapper functions generated by `bindActionCreators` will automatically forward all of their arguments, so you don't need to do that by hand.
Since the `mapDispatchToProps` argument is supplied, the component will no longer receive the default `dispatch`. You may bring it back by adding it manually to the return of your `mapDispatchToProps`, although most of the time you shouldn’t need to do this:
256
+
If the `mapDispatchToProps` argument is supplied, the component will no longer receive the default `dispatch`. You may bring it back by adding it manually to the return of your `mapDispatchToProps`, although most of the time you shouldn’t need to do this:
256
257
257
258
```js
258
259
import { bindActionCreators } from"redux";
@@ -261,14 +262,7 @@ import { bindActionCreators } from "redux";
@@ -387,7 +374,7 @@ There are discussions regarding whether to provide `dispatch` to your components
387
374
388
375
### Can I `mapDispatchToProps` without `mapStateToProps` in Redux?
389
376
390
-
Yes. You can skip the first parameter by passing `undefined` or `null`. Your component will not subscribe to the store, and gain dispatch props defined by `mapStateToProps`.
377
+
Yes. You can skip the first parameter by passing `undefined` or `null`. Your component will not subscribe to the store, and will still receive the dispatch props defined by `mapStateToProps`.
391
378
392
379
```js
393
380
connect(
@@ -398,7 +385,7 @@ connect(
398
385
399
386
### Can I call `store.dispatch`?
400
387
401
-
It is an anti-pattern to use `store.dispatch` which you may retrieve from the `store` in context. As explained in [Redux's FAQ on store setup](https://redux.js.org/faq/storesetup#can-or-should-i-create-multiple-stores-can-i-import-my-store-directly-and-use-it-in-components-myself), any direct access of the store in a component is not recommended, whether it be via direct import or from context. Let React-Redux’s `connect` handle the access to the store, and use the `dispatch` it passes to the props to dispatch actions.
388
+
It's an anti-pattern to interact with the store directly in a React component, whether it's an explicit import of the store or accessing it via context (see the [Redux FAQ entry on store setup](https://redux.js.org/faq/storesetup#can-or-should-i-create-multiple-stores-can-i-import-my-store-directly-and-use-it-in-components-myself) for more details). Let React-Redux’s `connect` handle the access to the store, and use the `dispatch` it passes to the props to dispatch actions.
0 commit comments