M
M
Maxim Korolsky2016-06-27 15:35:29
React
Maxim Korolsky, 2016-06-27 15:35:29

How to optimize mapDispatchToProps?

In order not to fence in each component like this

const mapDispatchToProps = (dispatch) => {
  return {
    saveUser: bindActionCreators(saveUserAction, dispatch),
    getUser: bindActionCreators(getUserAction, dispatch),
    getUser: bindActionCreators(getUserAction, dispatch),
    getUser: bindActionCreators(getUserAction, dispatch),
    getUser: bindActionCreators(getUserAction, dispatch),
    getUser: bindActionCreators(getUserAction, dispatch),
                и т.д.
  };
};

So far, it only came to mind to do all this in the action, and then only pass this function from the action ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2016-06-27
@SuperPosan

There is a good option: connect actions via *

import * as UserActions from '../actions/UserActions'
...
const mapDispatchToProps = (dispatch) => {
  return {
    userActions: bindActionCreators(UserActions, dispatch)
  }
}

In this case, all user actions (namely, all exported functions from the UserActions file) will be available from this.props.userActions (for example, this.props.userActions.saveUser)
Also, you can not write return if you return an object ( just our case)
const mapDispatchToProps = (dispatch) => ({
  userActions: bindActionCreators(UserActions, dispatch)
})

Notice the parentheses.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question