bcherny / undux

⚡️ Dead simple state for React. Now with Hooks support.

Home Page:https://undux.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can one update a store value from an asynchronous, external source?

kevinfrei opened this issue · comments

I'm horsing around with Electron and am trying to use Undux (because Redux boilerplate for a full Electron app was seriously killing me). Right now I'm just doing a bunch of proof-of-concept work. A key problem I've hit is that I need to be able to update some state in the renderer process (the thing drawing the UI) when something in the main process (the thing running NodeJS) sends it an asynchronous message to the global handler (logically, thing something is listening on the window object) My google-fu is failing me, because it really wants to show me "re"dux examples.

In Redux, it's very easy: you call store.dispatch(action) from wherever you want to dispatch an action. With Undux, I can't seem to figure out how to gain access to the store.set API from outside of a React component...

Hey there! What does the API for listening for messages from the main process look like? Could you listen from within a React component?

I've been battling where the nodejs API's are visible, unfortunately. I can surface the event emitter in the window object, but can't seem to get the assignment to occur until after initial render. I've been trying to avoid just putting in some random delay, but I might have to do that... I'll keep at it. I was hoping I was just missing something semi-obvious...

FIgured it out. It's a 3 step process, which is delightful, but works. I attach the ipcRenderer emitter object on the window, and from the (create-react-app created) index.js file, instead of rendering the root <App/>, I attach the function to do that on the window object as well. Once the DOM is initialized, the renderer then invokes the code to render <App/>, ensuring that the AsyncDoodad (invisible) React component can wire up the on(...) message receiver, and in there, I can access store.get/store.set! And if someone wants to see it (and maybe tell me I should leave the actual coding up to the people I manage :) ), it's at https://github.com/kevinfrei/music/ (which will hopefully turn into a cross-platform music player at some point in the next few months...)