Conversation
|
Why not have the keys be |
|
I had a similar but a slightly different approach. Check my implementation of useSync(). Do you think that it is worth creating an RFC for? |
|
See react-use createGlobalState. |
|
@wmzy You'll still have to import the hook into different components in order to use it, which is not ideal. A subscription based one with a unique identifier (either a string or a symbol) will be way handier and will cause less headache when trying to remember which hook to import and where to import it from. |
|
Libraries like How would Example using
|
|
@bennoinbeta And why going for third party solutions if React already had this built-in? Given how crucial this is and how simple it is to implement, it makes sense to have it included in core. |
|
I can suggest another way, like a selector ( a bit looks like useSelector in redux)
Returns:
Note: I have a full implementation of this. Since most people agreed that we need a |
|
This seems unnecessary given that you can simply
Indeed, Zustand+Context achives exactly that, so you don't even need to write it yourself: https://tkdodo.eu/blog/zustand-and-react-context |
|
Isn't it cool to use a global state just like useState const [name, setGlobalState] = useGlobalState(s => s.name); Set name to global state: Simple and humble.. right? The problem with context is that it is not that optimized on render control. Update one value in context, all the components which uses any other part of context too will re render. |
This is the global state implemented using batch updates. The usage is similar to useState, simpler than context, and the usage scenarios are different.
Here is my implementation and usage example.