dai-shi / react-hooks-global-state

[NOT MAINTAINED] Simple global state for React with Hooks API without Context API
https://www.npmjs.com/package/react-hooks-global-state
MIT License
1.1k stars 62 forks source link

setGlobalState cannot be called during first render? #17

Closed vans163 closed 5 years ago

vans163 commented 5 years ago

It seems if I call during the first render setGlobalState, it does not proc. But useGlobalState works. Maybe setGlobalState gets queued until the end of render, in which case I need to set something before render is done.

dai-shi commented 5 years ago

Oh, good catch. Let me see...

dai-shi commented 5 years ago

Yeah, it's queued. Thought it shouldn't be a problem because we don't expose state like getGlobalState, ...until I saw your hack in #15. setGlobalState was added later to the API, which I now think, in a bad manner. It's easy to break things.

Could you let me know your use case when you need to setGlobalState on startup?


And I realize a possible bug in the case of createGlobalStore. 😱

dai-shi commented 5 years ago

The queuing was the bad idea of mine. Fixed now. It should work both with setGlobalState and dispatch. The test code was also added.

About reading global state, let's continue discussion in #15.

dai-shi commented 5 years ago

Published: https://www.npmjs.com/package/react-hooks-global-state/v/0.11.0

There might still be some edge cases, especially with react-hot-loader. Feedback is welcome.

Thanks again! @vans163

dai-shi commented 5 years ago

setGlobalState was added later to the API, which I now think, in a bad manner.

Correction: It's not bad, but it has to be used carefully. The API doesn't force it and there's no good document.