Combining Zustand with React Query

Sdílet
Vložit
  • čas přidán 8. 09. 2024
  • 🚀 Project React → cosden.solutio...
    📥 Import React (Newsletter) → cosden.solutio...
    Source Code → github.com/cos...
    Join The Discord! → discord.cosden...
    VSCode Theme | Font → Material Theme Darker | Menlo, Monaco "monospace"
    In this video I will show you the correct way to combine Zustand with React Query, two very popular libraries that don't even need to be combined together because of how good they are! But they definitely can if you need them to. Just be careful because there is a right way to do it, and a wrong way to do it. I will show you both.

Komentáře • 82

  • @xanteI
    @xanteI Před měsícem +70

    Destcruturing elements from zustand store will lead to rerenders if something in the store is changed, even if you don't destructure it. Better to do it like: const filters = useUserStore((state) => state.filters)

    • @TannerBarcelos
      @TannerBarcelos Před měsícem +2

      Didn’t even know. Going to push some fixes to my repo at work tomorrow.

    • @sarcasticdna
      @sarcasticdna Před 26 dny

      yes that is true I learnt it the hard way

    • @ahmedabdelrahman4034
      @ahmedabdelrahman4034 Před 19 dny

      you could use useShallow to prevent such behavior if you want to destructure the states

  • @pedroserapio8075
    @pedroserapio8075 Před měsícem +4

    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 Před měsícem +6

    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

  • @joseeavr
    @joseeavr Před měsícem +1

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

  • @FuzzyAnkles
    @FuzzyAnkles Před měsícem +2

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

  • @rjwhite4424
    @rjwhite4424 Před měsícem

    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.

  • @joeprince7509
    @joeprince7509 Před měsícem

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

  • @mDHARYL
    @mDHARYL Před 18 dny

    This is what I've been looking 😊 thank you

  • @TannerBarcelos
    @TannerBarcelos Před měsícem +2

    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 Před měsícem

      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 Před měsícem +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 Před měsícem

      @@TannerBarcelos ok great advice thank you.

  • @geforcesong
    @geforcesong Před měsícem

    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  Před měsícem +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 Před měsícem

      @@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 Před měsícem

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

  • @juancarlostremolss3158
    @juancarlostremolss3158 Před měsícem

    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.

  • @sakosa8784
    @sakosa8784 Před měsícem

    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_ Před dnem

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

  • @StefanStankovic-i9c
    @StefanStankovic-i9c Před měsícem

    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

  • @danielandresogodieme2329
    @danielandresogodieme2329 Před měsícem

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

  • @sajibdas6708
    @sajibdas6708 Před 18 dny

    thank you

  • @ethanmenezes
    @ethanmenezes Před měsícem +2

    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?

  • @henochangemichaellonzokoff6036

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

    • @geraldschlader9159
      @geraldschlader9159 Před měsícem +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 Před měsícem +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_ Před dnem

      Just look into Shadcn data table it uses tanstack table

  • @otis2646
    @otis2646 Před 25 dny +1

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

  • @ishandeveloper
    @ishandeveloper Před měsícem

    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  Před měsícem +1

      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 Před 22 dny

      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

  • @StefanStankovic-i9c
    @StefanStankovic-i9c Před měsícem

    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

  • @Mirislomovmirjalol
    @Mirislomovmirjalol Před měsícem +6

    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 Před měsícem +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  Před měsícem +3

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

    • @Mirislomovmirjalol
      @Mirislomovmirjalol Před měsícem

      @@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 Před měsícem

      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 Před 22 dny

      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.

  • @HiagoMM
    @HiagoMM Před měsícem

    I do exatly this proposal to my team past week rsrsrs

  • @dhirendraKVlogOfficial
    @dhirendraKVlogOfficial Před měsícem

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

  • @irfansaeedkhan7242
    @irfansaeedkhan7242 Před měsícem

    practical knowledge thank you

  • @akashddeepchitransh4537

    What if I want to implement filtering on the client.

  • @_aoKhanhMinh
    @_aoKhanhMinh Před měsícem

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

  • @linqusik
    @linqusik Před měsícem

    How have you done this beautiful cursor animation?

  • @anth0ni33
    @anth0ni33 Před měsícem +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.

  • @candyman3537
    @candyman3537 Před 25 dny

    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?

    • @otis2646
      @otis2646 Před 25 dny

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

    • @otis2646
      @otis2646 Před 25 dny

      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  Před 25 dny

      check docs, staleTime and gcTime are your friends

  • @lsaavedr
    @lsaavedr Před měsícem

    thanks!!! ❤

  • @Stefoux
    @Stefoux Před měsícem

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

  • @smoothdvd
    @smoothdvd Před měsícem

    Zustand is "global" state management

  • @shikamarunara2582
    @shikamarunara2582 Před měsícem

    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  Před měsícem

      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 Před 12 hodinami

      @@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?

  • @teddyTED2oo7
    @teddyTED2oo7 Před měsícem

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

  • @niklausmikaelson7332
    @niklausmikaelson7332 Před 27 dny

    Can we update the state with mutation

  • @nhatminhhoang1469
    @nhatminhhoang1469 Před měsícem

    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  Před měsícem

      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 Před měsícem

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

  • @Brahmanity
    @Brahmanity Před měsícem

    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

  • @mohitxnova
    @mohitxnova Před měsícem

    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  Před měsícem +1

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

    • @mohitxnova
      @mohitxnova Před měsícem

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

  • @user-tq7vs1dp2u
    @user-tq7vs1dp2u Před měsícem

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

    • @diegofreeman9620
      @diegofreeman9620 Před měsícem

      Its useMutation that has onSuccess

    • @dustshooter5692
      @dustshooter5692 Před měsícem

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

    • @z-aru
      @z-aru Před měsícem

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

  • @chukwukaeze4736
    @chukwukaeze4736 Před měsícem

    Noiccceee

  • @joshuasingh854
    @joshuasingh854 Před měsícem

    Is this the same for SWR?

  • @xiiEJRAM
    @xiiEJRAM Před měsícem +1

    Jotai is better and less complex

  • @vsvid
    @vsvid Před 15 dny

    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

  • @extradipofhoney6189
    @extradipofhoney6189 Před měsícem

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

  • @fabricamarciana
    @fabricamarciana Před měsícem