How to optimize mapDispatchToProps?

That would be in each component is not a city that is
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),
 etc.
};
};


While came to mind but all of this action has yet to do, and then only this function from the action to tell...
July 8th 19 at 11:39
1 answer
July 8th 19 at 11:41
Solution
There is a good option: connect using actions *

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


In this case, all actions of the user (namely, all functions that are exported from the file actions toolbar) will be available from this.props.actions toolbar (for example, this.props.actions toolbar.saveUser)

Also, you can not write return, if you return an object (just our case)
const mapDispatchToProps = (dispatch) => ({
 actions toolbar: bindActionCreators(actions toolbar, dispatch)
})


Pay attention to parentheses.
thank you good solution - Audrey.Bergstrom commented on July 8th 19 at 11:44
: please, if everything is OK - mark it as a solution. - dannie.Stehr commented on July 8th 19 at 11:47
: can you explain what exactly makes the method bindActionCreators? - danny43 commented on July 8th 19 at 11:50
: wrap each function in dispatch(function) - https://github.com/reactjs/redux/blob/64a8c66b4541... And when we export using *, we have the object with functions (https://monosnap.com/file/cQO4YJiO7vajL1cDjq7AlZuf...in this case, bindActionCreators wraps every function in the dispatch(function) https://github.com/reactjs/redux/blob/64a8c66b4541... - dannie.Stehr commented on July 8th 19 at 11:53
have monosnap in the link brackets should be removed, right https://monosnap.com/file/cQO4YJiO7vajL1cDjq7AlZuf... - dannie.Stehr commented on July 8th 19 at 11:56
and not thunk it is easier to attach and send immediately dispatch(action())? Or is it not too sheer? - danny43 commented on July 8th 19 at 11:59
: if we are talking about redux-thunk, it's not the same. Redux-thunk (middleware) checks if the input came from function, she turns to the arguments appear dispatch and getState, and if an object (that is caused by the usual dispatch -> {type: 'ACTION_NAME' }) bind on. - dannie.Stehr commented on July 8th 19 at 12:02
: then it turns out that the code of the author
const mapDispatchToProps = (dispatch) => {
 return {
 actions toolbar: bindActionCreators(actions toolbar, dispatch)
}
}


absolutely equal ?

const mapDispatchToProps = (dispatch) => {
 return {
 actions toolbar: (a, b) => dispatch(actions toolbar(a, b))
}
}


Why in such cases do we need it? - danny43 commented on July 8th 19 at 12:05
although the difference is probably only that bindActionCreators maybe as You said to accept and process an object module with action creatom. Right? - danny43 commented on July 8th 19 at 12:08
: Yes, right. - dannie.Stehr commented on July 8th 19 at 12:11
: thanks for the clarification. - danny43 commented on July 8th 19 at 12:14

Find more questions by tags ReactRedux