-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
mapDispatch
docs
#1066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mapDispatch
docs
#1066
Conversation
Deploy preview for react-redux-docs ready! Built with commit 7794a2f |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great so far - almost there! Let's get these changes wrapped up and merge it!
id: connect-dispatching-actions-with-mapDispatchToProps | ||
title: Connect: Dispatching Actions with mapDispatchToProps | ||
hide_title: true | ||
sidebar_label: Connect: Dispatching Actions with mapStateToProps |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sidebar_label: Connect: Dispatching Actions with mapStateToProps | |
sidebar_label: Connect: Dispatching Actions with mapDispatchToProps |
Yay, copy-paste issues :)
|
||
The `mapDispatchToProps` functions are normally referred to as `mapDispatch` for short, and of course the actual variable name doesn’t matter in real code. | ||
|
||
## With or Without `mapDispatchToProps` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## With or Without `mapDispatchToProps` | |
## Approaches for Dispatching |
- By default, a connected component receives `props.dispatch` and can dispatch actions itself. | ||
- `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. | ||
|
||
The `mapDispatchToProps` functions are normally referred to as `mapDispatch` for short, and of course the actual variable name doesn’t matter in real code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The `mapDispatchToProps` functions are normally referred to as `mapDispatch` for short, and of course the actual variable name doesn’t matter in real code. | |
The `mapDispatchToProps` functions are normally referred to as `mapDispatch` for short, but the actual variable name used can be whatever you want. |
} | ||
``` | ||
|
||
### Providing A `mapDispatchToProps` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### Providing A `mapDispatchToProps` | |
### Providing A `mapDispatchToProps` Parameter |
#### More Declarative | ||
|
||
First, encapsulating the dispatch logic into function makes the implementation more declarative. | ||
Dispatching an action and let the Redux store handle the data flow is _how to_ implement the feature, it is not _what_ it does. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dispatching an action and let the Redux store handle the data flow is _how to_ implement the feature, it is not _what_ it does. | |
Dispatching an action and letting the Redux store handle the data flow is _how to_ implement the behavior, rather than _what_ it does. |
return { | ||
dispatch, | ||
...bindActionCreators( | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collapse object.
|
||
// or | ||
export default connect( | ||
null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null, | |
mapState, |
Might as well be consistent.
return { | ||
dispatch, | ||
...bindActionCreators( | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collapse object :)
(Hey, you were consistent :) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, that's my prettier 😅
|
||
### Can I `mapDispatchToProps` without `mapStateToProps` in Redux? | ||
|
||
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`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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`. | |
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`. |
|
||
### Can I call `store.dispatch`? | ||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. | |
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. |
Other side note, maybe for another PR: I'd really like to have the generated URLs be shorter, like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more tweaks:
}; | ||
``` | ||
|
||
You may take this chance to forward arguments to your action creators: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may take this chance to forward arguments to your action creators: | |
You will also likely want to forward arguments to your action creators: |
|
||
1. A **`function`** (an action creator) or an **`object`** (each field an action creator) | ||
2. `dispatch` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The wrapper functions generated by `bindActionCreators` will automatically forward all of their arguments, so you don't need to do that by hand. |
const reset = () => ({ type: "RESET" }); | ||
|
||
// binding an action creator | ||
// returns () => dispatch(increment()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// returns () => dispatch(increment()) | |
// returns (...args) => dispatch(increment(...args)) |
Can maybe skip doing this for the rest of them?
); | ||
// returns | ||
// { | ||
// increment: () => dispatch(increment()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh, let's go ahead and show all the ...args
in these comments after all.
Also, I am so excited to get this page published soon :) |
9a14bef
to
7794a2f
Compare
Awesome. Do it! |
* Consistent naming for `mapStateToProps` * Add `mapDispatch` docs * Doc site configuration changes * Review resolutions
What does this PR do?
New doc piece regarding #1001:
_Connect: Dispatch Actions with
mapDispatchToProps
Summary of the Changes
mapDispatchToProps
docsmapDispatchToProps
to sidebarmapStateToProps
names for consistency, add a small sectionsection to the
mapState
doc also for consistency