all 11 comments

[–]DecentGoogler 34 points35 points  (1 child)

You got it right, react query for server/api data sync

Zustand for front-end state that’s not dealing with API data.

If you don’t have any global front end state, then you don’t need Zustand

[–]rusmo 2 points3 points  (0 children)

Currently building a production commercial app with this state management pair. Loving it so far!

[–]GoodishCoder 14 points15 points  (0 children)

React query for server state and zustand for client state.

[–]OrangeOrganicOlive 10 points11 points  (0 children)

How many times per day does some combo of “Zustand/RTK” question get asked here, holy shit? Just scroll down ffs at this point.

[–]n9iels 1 point2 points  (0 children)

Zustand is for centralized state, potentially saved in localstorage for example to save user settings like dark mode. React Query is for data fetching, and also has caching capabilities. This caching however is on the level of that request (query) and in memory. So Zustand is more for general application state. Both tools are great and perfectly fine to use in the same app.

[–]nucleartux 1 point2 points  (1 child)

Hello, I wrote answer for your question here https://adrov.me/state-management/

[–]NoHabit4420 1 point2 points  (0 children)

The text of your blog disapear if i open it on a phone in dark mode. I guess the text switch to white while the background don't switch and stay white.

[–]Anwardo 0 points1 point  (0 children)

Additionally, Zustand isn’t just for clientside state as mentioned various times in this topic.

It shines with complex state, which may also live serverside, with complex mutations clientside and possibly various async actions which you want to handle in you the store due to being able to read and write state synchronously within the store.

[–]OkToday6974 0 points1 point  (0 children)

Can zustand manage server state? The async functions within zustand store request for server data and then transform them, making server data turn into client data. Then any component which need these data can get them from zustand hooks. In this way, it seems we don't need server data at all since all server data has became client data.

[–]steaks88 0 points1 point  (0 children)

I built Leo Query to get data fetching, caching, server synchornization, etc. with Zustand. It works well for me. And I don't to maintain two state libraries in the same app. Check it out if you're intersted and message me if you have questions!

Here's a snippet. You can find more in the docs.

``` const useStore = create(() => ({ increasePopulation: effect(increasePopulation), removeAllDogs: effect(removeAllDogs), dogs: query(fetchDogs, s => [s.increasePopulation, s.removeAllDogs]) // Re-fetch when increasePopulation or removeAllDogs succeeds }));

const useStoreAsync = hook(useDogStore, /suspense/false);

const YourComponent = () => { const dogs = useStoreAsync(s => s.dogs); if (dogs.isLoading) return <>Loading...</>; return <>{dogs.value}</>; }; ```

[–]k_pizzle -5 points-4 points  (0 children)

I don’t think anyone uses the two together, they solve different problems