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

Be able to access multiple state keys with a single hook #47

Closed natew closed 2 years ago

natew commented 4 years ago

I know this would change the library quite a bit, but I'm in search of a decent state system that would let me build my own state abstraction, and since you've spent so much time thinking about and working with concurrent mode, I'd prefer to lean on something that's been tested thoroughly.

You can see me explaining it here and better example code is here.

What I'd need basically is a way to use a single hook, but be able to access as many state keys as necessary throughout the render and have it "track". So something like this:

const state = useGlobalState(stateObject)

state.someValue // access
state.someValue = 1 // set

It's similar to your react-tracked, but using a class first syntax instead of reducer. I've actually written it already over Recoil fully, but Recoil fails at HMR. I may even take a stab at fixing Recoil HMR, but I do think Recoil seems to be overkill and they are heading in a very complex direction from what I can see, while not prioritizing a lot of community requests at all (explicitly not prioritizing them).

dai-shi commented 4 years ago

Hi, I don't fully read your issues in Recoil repo, but here's some comments for discussion and clarification.

There are many aspects in CM. What I'm concerning is "tearing" and "state branching". react-hooks-global-state doesn't support state branching, because it's an external store. (I tried a hack in v1 but will drop it in v2.)

state.someValue // access
state.someValue = 1 // set

This style is pretty hard for CM. You would need a custom setter or a proxy to support CM. Maybe you are already doing so.

What's the issue if you use react-tracked for your lib?

You might be interested in my another library use-atom, but it's less stable than react-tracked.

react-hooks-global-state v2 will also have an atom abstraction, but again it's an external store like Redux.

Just in case, you have seen the comparison table in https://github.com/dai-shi/will-this-react-global-state-work-in-concurrent-mode , haven't you?

natew commented 4 years ago

Ah, should have found use-atom, I had even ran across it before. I’ll give it a try.

In practice I wouldn’t use store.x = 1 I’d have the preferred default be to enforce having it behind a function.

And yea using a proxy. And have seen the guide, it’s great. Will let you know how it goes.

dai-shi commented 2 years ago

v2 is released and closing this.