Is mapDispatch2Props really necessary?
See original GitHub issueI’ve searched for this topic but none result matched the case I’m going to talk about.
In react-redux
, we use mapDispatch2Props
to inject access to dispatch
into components(containers) like:
// container.js
onSubmit() {
this.props.addUser(this.state.formData);
}
function mapDispatch2Props(dispatch) {
return {
addUser(data) {
dispatch({ type: ADD_USER, data });
}
}
}
However dispatch
is actually accessible anywhere as long as you import the store, so we don’t need this injection.
// utils.js
import store from '../store';
export function addUser(data) {
store.dispatch({ type: ADD_USER, data });
}
// containers.js
import { addUser } from '../utils';
onSubmit() {
addUser(this.state.formData);
}
I know this seems breaking the “all data should come in as props” rule of React, but importing and using util functions are very common. Say we need to display a timestamp in some specific format:
import { formatTime } from '../utils';
render() {
return (
<span>{formatTime(this.props.time)}</span>
);
}
And this approach prevents your component(container) from being aware of redux stuff. So I wonder if it’s a good replacement of mapDispatch2Props
, especially when not taking ownProps
.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
You Might Not Need The mapDispatchToProps Function
One little-known feature of React-Redux is that mapDispatchToProps can just be a plain object. It doesn't have to be a function. ... The...
Read more >mapStateToProps, and why you may not need ... - Medium
That you don't need at first. The only way to update data in your store is to dispatch actions. Well, something only a...
Read more >Why I'll definitely use mapDispatchToProps in Redux
In short, mapDispatchToProps is a function that maps the dispatch function and returns an object of functions, which will be merged into the ......
Read more >How does connect work without mapDispatchToProps
Can someone explain why in this case mapDispatchToProps is not needed here. As well, how is the function getting the dispatch function?
Read more >Connect: Dispatching Actions with mapDispatchToProps
Providing a mapDispatchToProps allows you to specify which actions your component might need to dispatch. It lets you provide action ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No, mDTP isn’t strictly necessary. You’re free to get access to
dispatch
or bind it to your action creators by other means, but for most it’s a nice convenience. Especially in the object shorthand form.Also please see the Redux FAQ entry on why we discourage importing the store directly.