FormidableLabs / freactal

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

Injecting new state asynchronously #97

Open crobinson42 opened 6 years ago

crobinson42 commented 6 years ago

I have a case where Firebase events are emitted asynchronously and new data needs to be injected into the state at any time.

With the exception of being jenky and using wrapComponentWithState() to inject new state, nothing jumps out right away.

In Redux parlance, you can do this in your app when it mounts to invoke "actions" at anytime to effect the state:

import store from './store'

sockets.on( 'new_data', data => store.dispatch({ type: 'NEW_DATA', payload: data }) )

How would one go about this use case? Thanks! Awesome project, btw!

agurtovoy commented 6 years ago

@crobinson42 AFAIK you can call effects at any point and they will modify the state as expected, so you should be able to subscribe to your Firebase events in your component's initialize and call effects.updateFromServer() (or however your call yours) with the new data.

crobinson42 commented 6 years ago

Thanks! I see that's the solution for using freactal inside the component context. I'm looking for a solution to affect state from outside a react component context.

On Feb 16, 2018 3:09 PM, "Aleksey Gurtovoy" notifications@github.com wrote:

@crobinson42 https://github.com/crobinson42 AFAIK you can call effects at any point and they will modify the state as expected, so you should be able to subscribe to your Firebase events in your component's initialize and call effects.updateFromServer() (or however your call yours) with the new data.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/FormidableLabs/freactal/issues/97#issuecomment-366384868, or mute the thread https://github.com/notifications/unsubscribe-auth/AFsmS8ATCHpSmZWhccJ0WEJr2x10qu7zks5tVgqxgaJpZM4R_I9f .

bingomanatee commented 6 years ago

the initialize event has access to the event registry; you could externalize the events into any context you want and call them whenever. (and/or do AJAX stuff there).

I'm assuming that you're happy with the react system on which state has been imposed still being where you SEE the effects.

crobinson42 commented 6 years ago

@bingomanatee Thx, I'll check that out.

I'm assuming that you're happy with the react system on which state has been imposed still being where you SEE the effects.

Not sure what you're assumption is related to or based on... care to share the relevance to problem?

bingomanatee commented 6 years ago

its easy to export the triggers (effects) but the expression of the result (state) will still be scoped to wherever you bind the state machine. so as long as you're cool with that you should be fine.

divmain commented 6 years ago

Going to close this as resolved.

Aside from what has already been mentioned, you could kick off an effect on componentDidMount, setup your listener there, and it'll have access to any effects you may want to invoke. Of course, you'll want to disable the listener on componentWillUnmount.

The benefit with this approach is the ability to subscribe to Firebase changes only when the component is mounted, which is often the behavior you'll want anyway.

Hope that helps. Please re-open if you have any further issues with this.