New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zustand 3 milestones #71
Comments
Unfortunately, I don't think we are 100% ready for it. The tearing issue can be solved by creating a deep copy of the store but correctly rendering after a priority interrupt seems to require a context provider. A context provider would break the ability to use Zustand outside of React and break transient updates. To solve this, things could be broken up into separate packages. The main package would be Zustand as we know it today (but with a context provider) for use with React. Another package would be the "core" state manager that has nothing to do with React, basically the |
The simpler API is definitely doable. I don't think it needs to be backwards compatible if we release it as a v3 update. |
Could you please add an option to add functions or other properties to base object? Maybe some api function for adding stuff? |
you can add them to the base object by doing base.foo = () => ... |
I think it would be cool if you could use zustand to create a singleton store from ANY hook that returns data. That way folks that have an existing custom hook managing non-singleton state, could easily upgrade it to be a singleton via zustand. Constate does this but I think zustand can do it better. So it essentially turns the clever way of sharing state without Context into its own separate feature, apart from the opinionated state management via the Using the Basic Example from Constate as a starter...
|
I was also wondering how this will play with the various data fetching libraries coming out that take advantage of Suspense such as SWR and React-Query. Right now zustand is not able to compose other hooks at all. It's sort of a dead-end hook because the API it exposes does not follow the rules of hooks. It's a proprietary API. I made a demo of how you might mix zustand with react-query but it feels a bit dirty :( I'd like to be able to have zustand actions BE react-query hooks |
Another idea for clean api would be to return the api object and have the hook be a property on it.
|
would be nice but its against hook naming convention, which means eslint would start complaining. must be useCamelCase unfortunately. personally i would prefer your version, but that's just asking for trouble going against linters like that. ;-) |
The reason I use zustand and i'm interested into collaborate is the absence of the context api. The context is not a good solution for global state management as it re-renders automatically every subscribers, and react-redux took a step back in its V7 from it. Some other libraries try to deal with it, with success (like react-tracked), but the fact that zustand is so light and efficient and doesn't depend of the context is its force. |
@rdhox You can read about and experiment with state managers and concurrent mode here. Zustand has a problem with "tearing during update", "tearing with transition", and "proper branching with transition". |
The "tearing" effect is caused by components using the same state without React knowing they are using the same state. In concurrent mode, React will do its best to update each component as fast as it can without dropping frames. It does this by interrupting renders. It seems React won't interrupt renders between components if they are using the same context. Zustand doesn't use context so React doesn't know to keep the components in sync. |
Looks like a new React hook might solve this: reactjs/rfcs#147 |
Thanks for your answers. I dug into it a little yesterday when I found the repo that you mentionned, and I reproduce the zustand example for convenience: https://codesandbox.io/s/jolly-tdd-vgcko I will continue to test and read to really understand the matter. In the sandbox, changing state with transition seems to work, only the change of state outside of react seems to be a problem. |
From what I understand from the example above, tearing happens when:
This behavior doesn't happen if the count is a local state of the parent component, or if count is a value from a context provider. |
Yeah that's exactly what happens and the |
@drcmda @JeremyRH Thank you for replying to one of my comments above. Did you have any thoughts on my other two comments above? Zustand composing other hooks e.g useSWR / React-Query: Zustand turning any hook into a shared hook, similar to Constate: (This one I feel would be difficult because zustand relies pretty heavily on the (set, get) architecture). |
When you use React-Query's
This would basically be the same thing as setting up a singleton but using a custom hook instead of an object with |
For fun I tried to implement the PR hooks
Of course we are far from a real new feature in production, but if you want to fiddle with it: |
After the few changes made by bvaughn on the useMutableSource hooks, I still have some difficulties to implement it, especially with the event outside the react app which still tear the states, like in the example above. Seems to work ok with the classic update and the useTransition hooks. |
Edit: The bug I have in the sandbox may be linked to the end of this post: reactjs/rfcs#147 (comment) |
FWIW, we plan v3 for the simpler api and v4 for concurrent mode. |
Some additional thoughts on API changes, might be worth considering for v3: #151 |
Hi, how would you want developers connecting zustand and reactSWR/query today? (for some cases where you need to fetch but still want it in a global store) is this a v4 only feature ? |
zustand is unopinionated how developers connect it with others. It's the same in v4. |
Let's collect some,
currently
why not
vanilla users wouldn't name it "useStore"
it would mean we're now api compatible with redux without doing much.
with a little bit of hacking it could also be made backwards compatible by giving it a iterable property. i've done this before with three-fibers useModel which returned an array once, but then i decided a single value is leaner.
@JeremyRH
The text was updated successfully, but these errors were encountered: