Combining Zustand with React Query

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025

ความคิดเห็น • 121

  • @pedroserapio8075
    @pedroserapio8075 6 หลายเดือนก่อน +15

    Much appreciated. I started implementing React Query on top of Zustand and started to think, why this sounds like duplicating data. Great and simple explanation.

  • @johndevnoza4223
    @johndevnoza4223 6 หลายเดือนก่อน +15

    as an entry level developer, with no work experience, i got that part at the very begining when i tryed tanstack query. i guess now i can be proud! :D great channel btw

  • @yakuznecov
    @yakuznecov 2 หลายเดือนก่อน +2

    Спасибо большое за великолепный урок. Я долго искал реализацию этой задачи, что бы просто было. И вы объяснили как нельзя лучше. Видно, что у вас огромный опыт. Успехов каналу в развитии.

  • @GameKornel
    @GameKornel 3 หลายเดือนก่อน +10

    Be careful with calling zustand this way. To properly "tree-shake" it you need to go with const data = useStore((state) => state.data), not just const { data } = useStore().
    Anyway great video!

  • @gauravraj8728
    @gauravraj8728 4 หลายเดือนก่อน +8

    Great video! However, instead of managing the filters using client state, I suggest using URL parameters. This approach helps in sharing URLs with specific filter settings, making it easier for users to bookmark or share filtered views.

  • @ronylev-ari3219
    @ronylev-ari3219 4 หลายเดือนก่อน

    Awesome tutorial. I rarely get to watch ones that are oriented towards real projects and touch on architecturing your application using the target tools and aiming to establish best practices.

  • @jsvrs
    @jsvrs 6 หลายเดือนก่อน +8

    Can't wait for the next video: Combining Redux with Zustand and React Query

    • @knowledgedose1956
      @knowledgedose1956 6 หลายเดือนก่อน +1

      смешно смешно 😂

    • @edu.paixao
      @edu.paixao 4 หลายเดือนก่อน +2

      Bundle size: 1GB 😂

  • @socaseinpoint
    @socaseinpoint 2 หลายเดือนก่อน

    Wow, that's exactly what i was doing, but you make it even more clear! Thanks!

  • @the_saliheen
    @the_saliheen 2 หลายเดือนก่อน +1

    Splitting the state into client and server adds another level of complexity for the component itself. Now your component has to differentiate between the two types, and it basically binds the two state types which imo is also a weird pattern.
    One can argue that the filter params are also a "server" state because that's how your server knows which data to return! And you see how it can be a point for argumentation within teams.
    Instead, what I'd do is decide to come up with the single "data" state which overlooks everything "under the hood" and exposes the data to the component, so the component itself can be less confusing and be cleaner.
    So in this example, Zustand can be a single point of reference for the component with its hooks, while the React Query can be encapsulated inside of Zustand's implementation.
    This way the component is really a presentational view component that doesn't know about "server" and "client". The result is a simpler code:
    Component

    • @filippobarcellos2944
      @filippobarcellos2944 26 วันที่ผ่านมา

      i like that approach but could you provide an example?

  • @danielandresogodieme2329
    @danielandresogodieme2329 6 หลายเดือนก่อน

    Hahah ! That video came out on point ! Thank you so much ! :)

  • @FuzzyAnkles
    @FuzzyAnkles 6 หลายเดือนก่อน +2

    Man im just building this now. Are you stalking me? :D Great content!

  • @mDHARYL
    @mDHARYL 5 หลายเดือนก่อน

    This is what I've been looking 😊 thank you

  • @名字不重要-v6u
    @名字不重要-v6u หลายเดือนก่อน +1

    Thanks for sharing! However, I am confused here. What if I need user information to be accessed over the whole project? I would choose to call user API in the layout when the user logged in and store the information in Zustand, so that every component can access user info. Therefore I optionally chose the "wrong way" mentioned in the video, calling API by tanstack query and storing the user info in Zustand, Does anyone have a better efficient way in this case?

    • @Shubham-yc6nz
      @Shubham-yc6nz 22 วันที่ผ่านมา

      I think you can call the react query function in multiple components. It don't make extra request usually using cache data

  • @Krishcode264
    @Krishcode264 5 หลายเดือนก่อน +2

    and what if we need state mutations in the data , dont we need to keep the client state for that ??

  • @rjwhite4424
    @rjwhite4424 6 หลายเดือนก่อน

    on the website the example they have is they make 2 types, one describing state and the other type describing actions then create the store saying create. It might be overkill but I thought it helped make the code a little easier to read. Especially if their are more than one actions assigned to a state.

  • @giorgos6576
    @giorgos6576 2 หลายเดือนก่อน +2

    The useEffect would be necessary if these data needed to be more global. In this example you just display them in the same component.

    • @dotRarufu
      @dotRarufu หลายเดือนก่อน +2

      its not necessary, data can just be fetched again from other components, and its fine since its cached

  • @dhirendraKVlogOfficial
    @dhirendraKVlogOfficial 6 หลายเดือนก่อน

    thanks for these videos for frontend development.
    I guess I am the first watching this video 😅

  • @afzalnomani7561
    @afzalnomani7561 3 หลายเดือนก่อน

    What if i want user data in more components ?

  • @ruijose4803
    @ruijose4803 หลายเดือนก่อน

    Thanks the explanation. Just one questions, let's suppose we're registering a user, does the user data input be considered a client state? And Can I use Zustand to cache them?

  • @geforcesong
    @geforcesong 6 หลายเดือนก่อน

    I have to say this is true for normal cases like you described. For some other cases, you must put the users to zustand by useEffect and make changes for client. For example, in the user list, you need display an active column with checkbox, and you can check/uncheck the checkbox then submit the changes. Imagine that you have more other fields need do the same. In these cases, you have to put the users to zustand firstly by useEffect, there is no good way to implement this with react query only.

    • @cosdensolutions
      @cosdensolutions  6 หลายเดือนก่อน +1

      You could just store the IDs of the selected users in Zustand and then check against that for the UI. No need to duplicate the users

    • @geforcesong
      @geforcesong 6 หลายเดือนก่อน +1

      @@cosdensolutions well you can have a try, like i said, if you have multiple fields need to be updated by clients and the list also have sorting and filtering functionalities. you will see the issue. To save it in the store, you can keep the list as single source of truth. Make your life much easier.

    • @nikhilpsathyanathan
      @nikhilpsathyanathan 6 หลายเดือนก่อน

      @@geforcesong tanstackw query is for state management , you don't need to make a duplicate state. just store selectionId, filter state in zustand

  • @joeprince7509
    @joeprince7509 6 หลายเดือนก่อน

    Great explanation, thank you for the clarification between the client and server state management!

  • @BowangLan
    @BowangLan 4 หลายเดือนก่อน +1

    I think the two solutions proposed in this video are trying to solve different problems. The first solution, storing users directly in zustand, is often used (for me at least) when you need to make “data” from react query globally accessible. The second solution is simply trying to add extra filtering states in addition to users and pass them to the backend API.

    • @MosesZhao-sp9pg
      @MosesZhao-sp9pg 4 หลายเดือนก่อน

      Yeah, I am actually dealing with the same problem right now, and am wondering if this is the right way to handle global data with react query using zustand

    • @JoelKallstrom-jp6gr
      @JoelKallstrom-jp6gr 4 หลายเดือนก่อน +1

      You can just call the RQ hook from anywhere though, right? If the provider wraps all your components. So it's already globally accessible.

    • @Yan-oe2mw
      @Yan-oe2mw 3 หลายเดือนก่อน +1

      yeah he didn't made the data available globally but i think that you can just fetch the data again anywhere u need, as longs as the query key is the same, it will retrieve from the cache and it will like share the same data across those components fetching only once, because if the key is cached it won't fetch again

  • @linqusik
    @linqusik 6 หลายเดือนก่อน +1

    How have you done this beautiful cursor animation?

  • @hey_you_123
    @hey_you_123 3 หลายเดือนก่อน

    thanks for this info, it would be great to have a better demo using several server components and client components "use client"/"use server" etc... that way we can see the flow and possible errors

  • @أميرالمؤمنينبنمحمدأكرم
    @أميرالمؤمنينبنمحمدأكرم หลายเดือนก่อน

    What if we need the user data in another component?

    • @luis.galicia
      @luis.galicia 7 วันที่ผ่านมา

      You can use useState and prop drilling or call rquery with the cache Keys yoy can get the data from cache instead of the servee

  • @akashddeepchitransh4537
    @akashddeepchitransh4537 5 หลายเดือนก่อน

    What if I want to implement filtering on the client.

  • @Arcane_Dragon878
    @Arcane_Dragon878 6 หลายเดือนก่อน

    I just so happen to find out about zustand
    Began using reacy query a while back
    And boom i find this video 5h after posting lets go

    • @AvikNayak_
      @AvikNayak_ 4 หลายเดือนก่อน

      Same bro😂. Zustand then react query and got recommendations for supabase

  • @lzyphil
    @lzyphil 2 หลายเดือนก่อน

    Hello, Could you please tell what plugin allows you to have the autocompletion code ? it looks really smart to know exactly what you want to code

    • @ketankumar5689
      @ketankumar5689 2 หลายเดือนก่อน +1

      copilot

    • @lzyphil
      @lzyphil 2 หลายเดือนก่อน

      @@ketankumar5689 thanks

  • @candyman3537
    @candyman3537 3 หลายเดือนก่อน

    I think there is still some use cases to store data in store. For example, when I try to share data between different screens. In your second method, there is no way to share the data. Am I right?

    • @Its-InderjeetSinghGill
      @Its-InderjeetSinghGill 3 หลายเดือนก่อน

      You can access data from cache using queryClient.

    • @candyman3537
      @candyman3537 3 หลายเดือนก่อน

      @@Its-InderjeetSinghGill But it's not reliable to access from cache. So I think saving in the store is still one of the best way to pass the data.

  • @ishandeveloper
    @ishandeveloper 5 หลายเดือนก่อน +1

    I liked the explanation, really crisp! However, my only concern is this that in case let's say this is supposed to be a shared global state within a big app. Let's say you're planning to store user's profile data or auth for a simpler example. Having a zustand store, from which you can retrieve the state value in any other component feels like a much more better approach, rather than adding react-query everywhere. Would love to know other opinions and thoughts?

    • @cosdensolutions
      @cosdensolutions  5 หลายเดือนก่อน +3

      React Query is meant to call the same query in many components, they highly recommend it! That's why the cache is there and it works great

    • @havokgames8297
      @havokgames8297 5 หลายเดือนก่อน +2

      You can wrap the react query call in a helper hook that you use around your app. Then you don’t need to repeat the cache key etc

    • @hassankamel755
      @hassankamel755 4 หลายเดือนก่อน

      Great 👍 ​@@havokgames8297

  • @juancarlostremolss3158
    @juancarlostremolss3158 6 หลายเดือนก่อน

    I like these types of video, that way I can send it instead of having to spend the time to actually explain best practices, before explaining key concepts. If you haven't done, can you also create a similar video but using query params instead of local state? Maybe also RSC patterns combined with client side code.

  • @andrevinsky
    @andrevinsky 2 หลายเดือนก่อน

    «Hoo-goh shtee-glitz» ©️ TSOO-shtang

  • @pepepecas7220
    @pepepecas7220 4 หลายเดือนก่อน

    onSuccess its deprecated on newer versions of tanstack query but you can use select to transform the data and (optionally) set it on the zustand state

  • @MosesZhao-sp9pg
    @MosesZhao-sp9pg 4 หลายเดือนก่อน

    Great Video! But I do want to ask what should we do if need to fetch the data that we got with react query globally? Thank!

  • @candyman3537
    @candyman3537 5 หลายเดือนก่อน

    Have one question, tanStack query will store data in cache. But what if I want to get data from server instead of cache since the data may change at any time?

    • @Krishcode264
      @Krishcode264 5 หลายเดือนก่อน

      good question , in next js we have revalidate but i dont know about this

    • @Krishcode264
      @Krishcode264 5 หลายเดือนก่อน

      and also i got one doubt related to this video : what if we need state mutations in the data , dont we need to keep the client state for that ??

    • @cosdensolutions
      @cosdensolutions  5 หลายเดือนก่อน

      check docs, staleTime and gcTime are your friends

  • @_aoKhanhMinh
    @_aoKhanhMinh 6 หลายเดือนก่อน

    Why check data in useEffect? Why do you check data into onSuccess function of ReactQuery?

  • @teddyTED2oo7
    @teddyTED2oo7 5 หลายเดือนก่อน

    are there any disadvantages if i use useQuery and axios together?

    • @cosdensolutions
      @cosdensolutions  5 หลายเดือนก่อน

      Not at all! I do it all the time

  • @ethanmenezes
    @ethanmenezes 6 หลายเดือนก่อน +1

    I am actually building a react native application and need help with managing state with firebase and the client app (native app) I wanna know if I can use this pattern?

  • @MikeMonji
    @MikeMonji 2 หลายเดือนก่อน +1

    Boy do you take time to make a point...

  • @shikamarunara2582
    @shikamarunara2582 6 หลายเดือนก่อน

    What to do incase, The server data I need requires in multiple components, Should I call the query in each of those components, or get it initially in some main component and store it in zustand?

    • @cosdensolutions
      @cosdensolutions  6 หลายเดือนก่อน

      Call the query in each component! That's what RQ is built for. It will fetch it once then return from the cache every time

    • @thomasgian3223
      @thomasgian3223 4 หลายเดือนก่อน

      @@cosdensolutionsWhat if I need to fetch the initial data with react query and then make some complex updates and use those data on multiple components? How to deal with this?

    • @hassankamel755
      @hassankamel755 4 หลายเดือนก่อน

      Custom Hook ​@@thomasgian3223

  • @niklausmikaelson7332
    @niklausmikaelson7332 5 หลายเดือนก่อน

    Can we update the state with mutation

  • @codealongwithkirk
    @codealongwithkirk 4 หลายเดือนก่อน

    Hi Darius, so for example, I am doing a CRUD function on that user. Basically, I update it and delete it, the initial state needs to be updated correspondingly. Do you think storing the users would be a good idea and just filter that data and change its updated object fields or remove it if it is being deleted? If no, what would be the best approach for this? TIA.

    • @Spaceod1
      @Spaceod1 13 วันที่ผ่านมา

      When you update or delete a user using a mutation, the best approach is to revalidate the list on success to fetch the latest details. If there’s an error, show it to the user. This way, you don’t have to manually update or filter the local state, and the data stays consistent with the backend.

  • @GuilhermeFaxina-r5q
    @GuilhermeFaxina-r5q 6 หลายเดือนก่อน

    does anyone know why react-query removed the onSuccess property in usequery?

    • @diegofreeman9620
      @diegofreeman9620 6 หลายเดือนก่อน

      Its useMutation that has onSuccess

    • @dustshooter5692
      @dustshooter5692 6 หลายเดือนก่อน

      since it would causes many problems, cons are bigger than pros

    • @z-aru
      @z-aru 6 หลายเดือนก่อน

      There is an explanation on tkdodo's blog as to why they removed the onSuccess from useQuery

  • @irfansaeedkhan7242
    @irfansaeedkhan7242 6 หลายเดือนก่อน

    practical knowledge thank you

  • @StefanStankovic-i9c
    @StefanStankovic-i9c 5 หลายเดือนก่อน

    Isn't it a better practice to do the "filter state management" via the URL? That way it remains persistent when refreshing the page, and when sharing the URL

    • @cosdensolutions
      @cosdensolutions  5 หลายเดือนก่อน +1

      yes it is, making a video on that soon

  • @HiagoMM
    @HiagoMM 6 หลายเดือนก่อน

    I do exatly this proposal to my team past week rsrsrs

  • @StefanStankovic-i9c
    @StefanStankovic-i9c 5 หลายเดือนก่อน

    Isn't it a much better practice to do the "filter state management" via the URL? That way it stays persistent when refreshing, and sharing the URL

  • @MeerUzairWasHere
    @MeerUzairWasHere 15 วันที่ผ่านมา

    THANKS

  • @henochangemichaellonzokoff6036
    @henochangemichaellonzokoff6036 6 หลายเดือนก่อน +6

    Can we have a video on react query, tanstack table and zustand to make a datatable?

    • @geraldschlader9159
      @geraldschlader9159 6 หลายเดือนก่อน +2

      actually there is Mantine React table and Material UI table that implement tanstack table very nicely and work well with react query. Also there is great documentation and examples.
      You would not need Zustand in the first step. Regarding the table if you want to preserve the filters, sorting and so on while the user navigates pages, that's when you need to think about how to preserve the state of the table, which may be desired or not.

    • @nikhilpsathyanathan
      @nikhilpsathyanathan 6 หลายเดือนก่อน +1

      make a custom hook that use react query to fetch data and pass it to tantack table and using zustand for holding state that hook return the table and loading state

    • @AvikNayak_
      @AvikNayak_ 4 หลายเดือนก่อน

      Just look into Shadcn data table it uses tanstack table

  • @lsaavedr
    @lsaavedr 6 หลายเดือนก่อน

    thanks!!! ❤

  • @nhatminhhoang1469
    @nhatminhhoang1469 6 หลายเดือนก่อน

    How about a derived state from server state ? Let's say I got users from the backend, but I want to keyBy it and use it in multiple components. What's the best way here please ?

    • @cosdensolutions
      @cosdensolutions  6 หลายเดือนก่อน

      You could use useMemo to create a derived array/object as you want, that way you save an extra render than if you use useEffect. Or you add the logic in the fetch fn

    • @zarefgamz2515
      @zarefgamz2515 6 หลายเดือนก่อน

      Where to use useMemo, I couldn't get it ?

  • @Mirislomovmirjalol
    @Mirislomovmirjalol 6 หลายเดือนก่อน +11

    i don't think your example is good for client state. for filtering, sorting, product variant options search param (in url) better choice then saving it in client state. Let me know if i am wrong

    • @latinrev3822
      @latinrev3822 6 หลายเดือนก่อน +1

      How are you're going to save your filtering state to pass it to the url? you need client state to save these filters.

    • @cosdensolutions
      @cosdensolutions  6 หลายเดือนก่อน +6

      If you use the URL then you don't need Zustand. But not everything needs to always go in the URL

    • @Mirislomovmirjalol
      @Mirislomovmirjalol 6 หลายเดือนก่อน

      @@latinrev3822 pagination? Rewrite url maybe? You can use useState for that. There is no point to use global state if your state gonna be used in just 1 component

    • @z-aru
      @z-aru 6 หลายเดือนก่อน

      You might probably want the search params as a default/initial value, not as a state value, since waiting for the url/search params to change might hinder your UI Component, for example controling a Switch Input active state.
      You still need client states for a better UI transition/active state.

    • @starrynight72
      @starrynight72 5 หลายเดือนก่อน

      I think you are right but maybe he merely wanted to show the point that we should not use client globle state management like zustand to store data from server when we already use react-query or swr to handle the server data management, although the case is not that proper.

  • @Brahmanity
    @Brahmanity 6 หลายเดือนก่อน

    guys if I use mobx do I even need react query or would it be better if I just make my queries in mobx methods

    • @geraldschlader9159
      @geraldschlader9159 6 หลายเดือนก่อน +1

      no, I would then stick to that

  • @sajibdas6708
    @sajibdas6708 5 หลายเดือนก่อน

    thank you

  • @joshuasingh854
    @joshuasingh854 6 หลายเดือนก่อน

    Is this the same for SWR?

    • @DuduHaupt
      @DuduHaupt 6 หลายเดือนก่อน

      probably yes

  • @regilearn2138
    @regilearn2138 4 หลายเดือนก่อน +1

    please do a video on redux toolkit and react Query

  • @TannerBarcelos
    @TannerBarcelos 6 หลายเดือนก่อน +4

    Use Zustand for anything related to UI like theme, colors, settings, etc.
    Use TSQuery for all your server state, I.e. data that comes from fetch calls to a backend, like a posts api that returns all posts in a social media app

    • @stanzy237
      @stanzy237 6 หลายเดือนก่อน

      Hey I have a question.
      Actually I felt in a situation where I make a post request to an APIs end points and I want to used the data from without passing in a zustant state. With a get request is very simple but I could not figure it out when making a post request

    • @TannerBarcelos
      @TannerBarcelos 6 หลายเดือนก่อน +1

      @@stanzy237 you can store those inputs in state and then define your mechanism for removing it. Also, you could return the input data / database output in your post response from the api and use it as well.
      But if you need to persist the posted data between renders or various interactions, then you will need to store it intermittently in Zustand

    • @stanzy237
      @stanzy237 6 หลายเดือนก่อน

      @@TannerBarcelos ok great advice thank you.

  • @mohitxnova
    @mohitxnova 6 หลายเดือนก่อน

    But does it make sense to use the store to save user data if i need the user data to be persistent through mmkv?

    • @cosdensolutions
      @cosdensolutions  6 หลายเดือนก่อน +1

      Yes, and then you'd pass it as initial data to RQ

    • @mohitxnova
      @mohitxnova 6 หลายเดือนก่อน

      @@cosdensolutions oh yess that’s actually great…never thought of it that way.

  • @anth0ni33
    @anth0ni33 6 หลายเดือนก่อน +1

    A lot of yapping IMO. I feel zustand has specific cases but to be used for filtering is not so good. Just stick it in the url.

    • @m-d-nabeel
      @m-d-nabeel 4 หลายเดือนก่อน

      I learned zero things from this vid, maybe negative if we include storing filters in other place instead of url

  • @Stefoux
    @Stefoux 6 หลายเดือนก่อน

    Darius ! I know you are completely fluente in French 😄🇫🇷

  • @Lukindyr
    @Lukindyr 2 หลายเดือนก่อน

  • @chukwukaeze4736
    @chukwukaeze4736 6 หลายเดือนก่อน

    Noiccceee

  • @alejandrojoseurielessalced423
    @alejandrojoseurielessalced423 4 หลายเดือนก่อน

    los videos se hacen completos, desde cero o sino no haces nada

  • @ColynBrown
    @ColynBrown หลายเดือนก่อน

    You don't explain what the problem is that the "naive" way tries to solve. Yes, its redundant data but unless a dev is trying to solve a particular problem by introducing zustand, I don't believe people are actually doing this. Is anyone actually doing that and why?

  • @xiiEJRAM
    @xiiEJRAM 6 หลายเดือนก่อน +1

    Jotai is better and less complex

  • @not_a_human_being
    @not_a_human_being 3 หลายเดือนก่อน

    you're using them "alongside", and not really "together".

  • @vsvid
    @vsvid 5 หลายเดือนก่อน

    What's the point of setting data from an API into another store? You lose the main purpose of RQ... RQ provides a 'global store' for your data under the guise of a cache. Such videos numb people

  • @vers85
    @vers85 4 หลายเดือนก่อน

    Sorry to say, but ihonestly dislike this video. You say there are two ways 'a good way' and a 'bad way' but those two cases are totally different. I totally agree there's no point in storing the readonly, server side data into zustand. But it's usually the case that you do it, because you fetch some data, go to some edit mdoe and then you edit it on the client before sending it back as a mutation back to the server. What then? The 'bad way' becomes a 'good way' all of the sudden?

  • @smoothdvd
    @smoothdvd 6 หลายเดือนก่อน

    Zustand is "global" state management

  • @extradipofhoney6189
    @extradipofhoney6189 6 หลายเดือนก่อน

    Nice video, but it could easily have been 5 mins shorter

  • @fabricamarciana
    @fabricamarciana 6 หลายเดือนก่อน