question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is mapDispatch2Props really necessary?

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
timdorrcommented, Sep 26, 2018

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.

0reactions
markeriksoncommented, Sep 26, 2018
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found