Question about mapDispatchToProps and bindActionCreators when multipla actions

33 views Asked by At

I learn React Redux and reading here about using mapDispatchToProps and bindActionCreators

It works fine the bindActionCreators when using it alone like so:

export default connect(null, (dispatch) =>
  bindActionCreators(actionCreators, dispatch)
)(FormContainer);

But I have more actions and tried this:

function mapDispatchToProps(dispatch) {
  return {
    bindActionCreators(actionCreators, dispatch),
    clearPosts: () => dispatch(clearPosts()),
  }
}

And that gives error like so:

enter image description here

. How can I combine them I read the docs and there's nothing as I can see about this please advice

2

There are 2 answers

0
Swift On BEST ANSWER

try below code

 function mapDispatchToProps(dispatch) {
      return {
        todoActions: bindActionCreators(actionCreators, dispatch),
        clearPosts: () => dispatch(clearPosts()),
      }
    }
0
phry On

It is recommended to use the "map object" notation nowadays if you really want to use connect.

const mapDispatchToProps = {
    ...actionCreators,
    clearPosts
}

But also, the recommendation is to use redux hooks instead of connect if you can use function components, as they are generally easier to use.

(If you were following a tutorial that was showing you connect, it might be outdated, please see the official redux tutorials instead)