Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Injecting new state asynchronously #97

Open
crobinson42 opened this issue Feb 8, 2018 · 6 comments
Open

Injecting new state asynchronously #97

crobinson42 opened this issue Feb 8, 2018 · 6 comments

Comments

@crobinson42
Copy link

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
Copy link
Contributor

@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
Copy link
Author

crobinson42 commented Feb 16, 2018 via email

@bingomanatee
Copy link

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
Copy link
Author

@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
Copy link

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
Copy link
Contributor

divmain commented Jul 6, 2018

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants