dai-shi / react-hooks-global-state

[NOT MAINTAINED] Simple global state for React with Hooks API without Context API

Home Page:https://www.npmjs.com/package/react-hooks-global-state

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

setGlobalState cannot be called during first render?

vans163 opened this issue · comments

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.

Oh, good catch. Let me see...

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. 😱

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.

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

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.