How can I call the generator function inside mapDispatchToProps?

This is a really simple JS question; I am sure that the problem is one of the areas. I want to do something similar, but this is the wrong syntax.

Basically, I want the event in my component to send different actions every time an event occurs. This whole approach may be wrong, in which case I would like to know how it should be done.

const mapDispatchToProps = dispatch => {
  return {
    function* getNextSection() {
      yield dispatch(local_actions.general)
      yield dispatch(local_actions.fixbugs)
      yield dispatch(local_actions.resumefinish)
    }
  }
}
+4
source share
1 answer

Interesting. The following code works:

const action1 => ({ type: 'action1' })
const action2 => ({ type: 'action2' })

function* actionGenerator() {
  yield action1()
  yield action2()
}

// A generator returns an iterator,
// it has to be stored in a variable
const actionIterator = actionGenerator()

const myAction = () => actionIterator.next().value

connect(mapStatetoProps, {
  myAction
})(MyComponent)

Then you can use it like:

this.props.myAction() // action1
this.props.myAction() // action2
+1
source

Source: https://habr.com/ru/post/1679093/


All Articles