Skip to content
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

Use mapDispatchToProps in react-redux#connect #91

Open
bschlenk opened this issue Apr 10, 2017 · 0 comments
Open

Use mapDispatchToProps in react-redux#connect #91

bschlenk opened this issue Apr 10, 2017 · 0 comments

Comments

@bschlenk
Copy link

bschlenk commented Apr 10, 2017

Instead of relying on the implicit dispatch prop being passed from a container to a component, utilize mapDispatchToProps to automatically bind actions to dispatch. For example, in PlayerContainer:

function mapStateToProps(state) {
  ...
  return {...};
}

export default connect(mapStateToProps)(PlayerContainer);

becomes

...
import { changeCurrentTime, changeSong, toggleIsPlaying } from '../actions/PlayerActions';
...

function mapStateToProps(state) {
  ...
  return {...};
}

const mapDispatchToProps = {
  changeSong,
  changeCurrentTime,
  toggleIsPlaying,
};

export default connect(mapStateToProps, mapDispatchToProps)(PlayerContainer);

And then inside Player:

  changeSong(changeType) {
    const { dispatch } = this.props;
    dispatch(changeSong(changeType));
  }

becomes

  changeSong(changeType) {
    this.props.changeSong(changeType);
  }

With redux-sound being my goto repo for inspiration when starting new react-redux projects, and very much being started as a learning project, I think it would be beneficial for there to be some examples like this within the code.

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

No branches or pull requests

1 participant