The Most Underrated State Management Tool in React

Поделиться
HTML-код
  • Опубликовано: 26 янв 2025

Комментарии • 71

  • @johng.weller468
    @johng.weller468 6 месяцев назад +57

    I have decided not to let the never-ending topic of state management in React overwhelm me. I have chosen to use React-Query for server state management and Zustand for the rest. This makes my life simpler.

    • @adamzalesak
      @adamzalesak 6 месяцев назад

      What do you mean by "the rest"? How do you distinguish between local and global state? And what about state boundary (reset state after a specific component is unmounted)?

    • @johng.weller468
      @johng.weller468 6 месяцев назад +6

      @@adamzalesak I'm not aware of every type of state that might exist in a react application, but at my current level, I do know that there is the server state and the client state; so in my comment, "the rest" was referring to the client state that includes the global state that doesn't depend on data from the server. In my opinion, every state that is only used inside a component to manage its internal data is a local state. If at any point another component in the application has to be aware of that state, I turn it into a global state, and voila. State boundary, 🤔I do not have any experience with that, so I'm still thinking 😅

    • @vinzz1786
      @vinzz1786 6 месяцев назад

      @@johng.weller468 great answer

    • @nizomsidiq3
      @nizomsidiq3 6 месяцев назад +5

      agreed, react-query for server, zustand for client global state, and just use context/state for component scoped state,

    • @johndevnoza4223
      @johndevnoza4223 6 месяцев назад +3

      I do exactly same and i think it is because im a begginer and always looking for short and easy way.

  • @devyb-cc
    @devyb-cc 6 месяцев назад +9

    react-query is truly a blessing for react community.

  • @Szchmausser
    @Szchmausser 5 месяцев назад +1

    I am using this approach to manage my global state with react-query, No need of React Context, or another library like Zustand. With only these 2 files and calling in consumer components, let me have the data in single source of truth. Thanks for the video!

  • @emmanueljulius1827
    @emmanueljulius1827 Месяц назад

    Cosden deserves a million subs. Big Ws man

  • @RonnieDenzel
    @RonnieDenzel 6 месяцев назад +5

    Eyyyyyy,codsens arguably first sponsor,congratulations🎉

  • @rockNbrain
    @rockNbrain 6 месяцев назад +3

    React-query is an awesome piece of tech, i'm a big fan of tanner and his crew... but using RQ like a global state management looks like a workaround, maybe it's better to use libs that were made for this purpose, like zustand, mobx and so on

  • @theoriginators1882
    @theoriginators1882 6 месяцев назад

    Splendid, and your course project react was great as well :)

  • @MarPhone123
    @MarPhone123 6 месяцев назад +2

    Hey there Cosden!
    This is how we manage our state in our company. One thing though, react query has a default cache time of 5 minutes, that means the data will be erased from the cache after that time, shouldn't we add a cacheTime: Infinity to the configuration params?
    Great video.

  • @hofimastah
    @hofimastah 6 месяцев назад +1

    Wow that's great! Updating your react query to v5 would also be a blast!

    • @jsward17
      @jsward17 5 месяцев назад

      Would the code be different?

    • @hofimastah
      @hofimastah 5 месяцев назад

      @@jsward17 it's not backward compatible

  • @melvins126
    @melvins126 6 месяцев назад

    Wow I will use this approach for sure. Thanks

  • @Onto.walking
    @Onto.walking 4 месяца назад

    I wanna watch every video of this channel and i am going to, this is premium content ❤

  • @lamhung4899
    @lamhung4899 6 месяцев назад +3

    Ppl talk too much about React Query + zustand, but then realize they become Redux toolkit technically 😂

  • @IsmailCherri
    @IsmailCherri 6 месяцев назад +1

    Why not use react-intl to store global constants? Or maybe msw to handle data manipulation? Even better, jest assertions to validate forms?

  • @Leandro-4687
    @Leandro-4687 6 месяцев назад +2

    Isn't the default cache time for React Query 5 minutes? If you want to use this strategy, wouldn't you need to set an infinite cache time to prevent data loss?

  • @ptolemyhenson6838
    @ptolemyhenson6838 6 месяцев назад +7

    What are the benefits of this? It seems like it's functionally just a global context provider, since the features specific to React Query are disabled explicitly.

    • @mrshambhuthapa
      @mrshambhuthapa 6 месяцев назад +2

      Data remains there on the state after refresh

    • @sebastianlpoliak
      @sebastianlpoliak 6 месяцев назад +2

      ​@@mrshambhuthapa data is not persisted between page refreshes if that's what you mean
      Between react context and RQ the only benefit is the improved way of handling the data updates. Apart from that I don't see a clear benefit and if you really need global state I guess there are better options that are meant for that like valtion, zustand or jotai

    • @Gokhan-er8qv
      @Gokhan-er8qv 6 месяцев назад

      @@sebastianlpoliak just use zustand its better than redux and this query shit

    • @Reactnativetech
      @Reactnativetech 6 месяцев назад

      Do you know it handle cache management for API. So if you has same component in multiple screen and it hit the API every time you visit the screen when you didnt use react-query which is not good especially when you have thousands or millions of users, your server will definitely be overloaded, that's where cache management is needed, for which react-query provides that solution, and there are many other benefits.

  • @markcampbell2491
    @markcampbell2491 2 месяца назад

    What happens if the user updates the state and then refreshes the browser?

  • @TheTeup
    @TheTeup 2 месяца назад

    Can context api be replaced by this?

  • @ਹੈਪ੍ਪੀਕੰਬੋਜ
    @ਹੈਪ੍ਪੀਕੰਬੋਜ 6 месяцев назад +1

    Sir, can we import custom hooks dynamically? When I try to import a custom hook dynamically, it gives me an error because it doesn't detect the imports used inside the custom hook.

    • @cosdensolutions
      @cosdensolutions  6 месяцев назад

      nope, custom hooks need to be known at render time. And they can't change order from one render to the next

  • @hallejujah
    @hallejujah 4 месяца назад

    it doesn't come from tanstack, it comes from heaven😮

  • @joeldaros
    @joeldaros 6 месяцев назад +14

    Please don’t do this. This is becoming a really big mess really quickly. Yes it works, but that’s the same as using a war tank to go to mall, you’ll get there sometime, but why?

  • @niklausmikaelson7332
    @niklausmikaelson7332 5 месяцев назад

    From past days i was looking for it how i can use it to manage data

  • @asankasiriwardena3383
    @asankasiriwardena3383 6 месяцев назад

    Does it support subscriptions or selectors similar to Zustand? I mean we need to nofity other components when a certain slice of state changes.

    • @Reactnativetech
      @Reactnativetech 6 месяцев назад

      I think it will rerender, and i think it will also behavior like useState or reducer when the state change it will rerender

  •  6 месяцев назад

    I don't really see the use in the if I'm being honest. That being said, it's a cool DX - and a nice exercise in generics and hook factories. I'd maybe look into just recreating signals or using custom events or something to recreate a global state solution instead so you can see how you don't really need that stuff from react query. All you need is a list of listeners and a way for them to subscribe to the state.
    Another thing you might have failed to consider is how this is (kind of unnessecarily) polluting react query's keys. It's cumbersome to maintain unique keys for every state if this is used actively, and especially not mixing those keys with existing query keys used in the query client for actual queries. Imagine also having a user query somewhere with the same "users" query key.

    • @doma7786
      @doma7786 6 месяцев назад

      I realized that with this comment. Thanks

  • @arnhazra
    @arnhazra 6 месяцев назад

    Can you make a video on event driven design with event emitter on react where we can pass events from one component and catch event in another. Thanks 😄

  • @slhliwwa5403
    @slhliwwa5403 6 месяцев назад

    broo do you have react projects in your course ??

    • @cosdensolutions
      @cosdensolutions  6 месяцев назад

      the whole course is one big project so yes :D

  • @4v4
    @4v4 4 месяца назад

    bro, i watch your videos on a tv. the font-size is soooo small

  • @nettemsarath3663
    @nettemsarath3663 6 месяцев назад

    Hey cosden, I have been working on javascript for the past 3 years, i used to write better code but iam always afraid of using typescript especially when integrating and using other 3rd party libraries or modules,
    Because I have to use their types which makes me Little afraid, so do u have any suggestions on this

  • @uselessandempty
    @uselessandempty Месяц назад

    "zustand hates this one simple trick"

  • @roninspect4357
    @roninspect4357 6 месяцев назад

    Some videos about nextjs please

  • @LukeCreates
    @LukeCreates 5 месяцев назад

    I see what you’re doing, but this seems like an insane approach… React Query is an _asynchronous_ state management library, so I’m not sure it’s great for this use case.

  • @MarlonEnglemam
    @MarlonEnglemam 6 месяцев назад

    I am trying to use react query to deal with API fetches and at the same time I need to store the data from the backend in states since there are a lot of changes that the user should be able to perform before sending the manipulated data to get updated in the backend. I'm currently just fetching the data with react query and saving the results in states inside of context API providers. I feel like I'm doing something wrong. Has anyone gone through the same? (next js 14 app)
    I didnt know I could achieve something familiar with react query.

    • @amaury_permer
      @amaury_permer 6 месяцев назад

      React query caches data by default, that's why it asks for a query key

    • @rockNbrain
      @rockNbrain 6 месяцев назад

      I think you don't need to use useState in this case, just use the data the RQ returns

  • @botyironcastle
    @botyironcastle 4 месяца назад

    why would I use react if I need a 3rd party library for everything...

  • @llanesluis19
    @llanesluis19 6 месяцев назад +1

    Why doing this when you have other state management solutions that were actually created with that purpose... I mean, I don't believe this offers any benefits and it's just a lot of unnecessary boilerplate. Idk, even TKDodo, the TanStack query mantainer recommends using this library only for async state and server state, and leave client state for libraries such as Zustand

  • @StingSting844
    @StingSting844 6 месяцев назад

    Nice video. But React query is one of the most used and well liked state management solutions for react. Its not underrated in any way

  • @kalideb-y3y
    @kalideb-y3y 5 месяцев назад

    why don't you just make the type of the "queryKey" also a generic type instead of uknown?

  • @brayanyevenes5954
    @brayanyevenes5954 6 месяцев назад

    Jotai && Zustand && TanStack Query

  • @zul.overflow
    @zul.overflow 6 месяцев назад

    Closure

  • @husniddinnorqulov7368
    @husniddinnorqulov7368 6 месяцев назад

  • @himanshumishra3425
    @himanshumishra3425 6 месяцев назад

    What if we want to persist some of the data

  • @tomasg6251
    @tomasg6251 5 месяцев назад

    super long briliant ad

  • @Xeras82
    @Xeras82 6 месяцев назад

    Disabling the default behavior of @tanstack/query indicates that you are abusing the library as state manager.

    • @cosdensolutions
      @cosdensolutions  6 месяцев назад

      nope, I disable that behavior all the time even just for fetching. It's meant to be configurable