FormidableLabs / freactal

Clean and robust state management for React and React-like libs.
MIT License
1.65k stars 46 forks source link

Access current state in effect #59

Closed etienne-dldc closed 7 years ago

etienne-dldc commented 7 years ago

Hi,

Is there a way to access the current state from inside an effect ?
My use case is a saveToRemoteStuff effect that would take a computed value from the state, send it with a POST request and then, when the POST succeed update the state with { dataSaved: true } (or something like this...) The problem is that I don't have access to the state in my effect 😢

computed: {
  dataToSave: ({ ... }) => (...)
},
effects: {
  saveToRemoteStuff: () => {
    return save(/* Can't access dataToSave here :/  */)
      .then(() => mergeIntoState({ dataSaved: true }))
}

My solution for now is to get computed value in the component and then pass it to the effect but it feels wrong.

computed: {
  dataToSave: ({ ... }) => (...)
},
effects: {
  saveToRemoteStuff: (effects, dataToSave) => save(dataToSave)
      .then(() => mergeIntoState({ dataSaved: true }))
}

Is there any better way to do this kind of things ? Ask me if you need more details :)

Etienne.

divmain commented 7 years ago

Hi @etienne-dldc! I think what you're describing is documented here in #29. Feel free to re-open if I'm incorrect!