Skip to content

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

Merged

Conversation

wgao19
Copy link
Contributor

@wgao19 wgao19 commented Oct 27, 2018

What does this PR do?

New doc piece regarding #1001:
_Connect: Dispatch Actions with mapDispatchToProps

Summary of the Changes

  • Add the mapDispatchToProps docs
  • Doc site: add mapDispatchToProps to sidebar
  • Align mapStateToProps names for consistency, add a small section
Arguments

1. state
2. ownProps

section to the mapState doc also for consistency

@netlify
Copy link

netlify bot commented Oct 27, 2018

Deploy preview for react-redux-docs ready!

Built with commit 7794a2f

https://deploy-preview-1066--react-redux-docs.netlify.com

Copy link
Contributor

@markerikson markerikson left a 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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(
{
Copy link
Contributor

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
null,
mapState,

Might as well be consistent.

return {
dispatch,
...bindActionCreators(
{
Copy link
Contributor

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 :) )

Copy link
Contributor Author

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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@markerikson
Copy link
Contributor

Other side note, maybe for another PR: I'd really like to have the generated URLs be shorter, like /docs/using-react-redux/mapdispatch or something. (Or even drop the /docs/ part.)

Copy link
Contributor

@markerikson markerikson left a 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// returns () => dispatch(increment())
// returns (...args) => dispatch(increment(...args))

Can maybe skip doing this for the rest of them?

);
// returns
// {
// increment: () => dispatch(increment()),
Copy link
Contributor

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.

@markerikson
Copy link
Contributor

Also, I am so excited to get this page published soon :)

@wgao19 wgao19 force-pushed the docs/dispatching-actions-with-mapdispatch branch from 9a14bef to 7794a2f Compare October 28, 2018 17:26
@markerikson
Copy link
Contributor

Awesome. Do it!

@markerikson markerikson merged commit 7458311 into reduxjs:master Oct 28, 2018
albertodev7 pushed a commit to albertodev7/react-redux that referenced this pull request Dec 8, 2022
* Consistent naming for `mapStateToProps`

* Add `mapDispatch` docs

* Doc site configuration changes

* Review resolutions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants