0

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

Kid
  • 1,869
  • 3
  • 19
  • 48
  • You would need ... before bindActionCreators but you don’t need to use bindActionCreators inside mapDispatchToProps as connect binds them for you. You can just do ...actionCreators instead. – Linda Paiste Apr 07 '21 at 11:04

2 Answers2

2

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)

phry
  • 35,762
  • 5
  • 67
  • 81
1

try below code

 function mapDispatchToProps(dispatch) {
      return {
        todoActions: bindActionCreators(actionCreators, dispatch),
        clearPosts: () => dispatch(clearPosts()),
      }
    }
Swift
  • 790
  • 1
  • 5
  • 19