blog/react-query-as-a-state-manager #67
Replies: 36 comments 14 replies
-
This article is fantastic. A job extremely well done.👏 |
Beta Was this translation helpful? Give feedback.
-
Great article! |
Beta Was this translation helpful? Give feedback.
-
Thanks 🙏 . Yes, I do that too sometimes @nikoskleidis, but not very often ;) |
Beta Was this translation helpful? Give feedback.
-
High quality article as usual! |
Beta Was this translation helpful? Give feedback.
-
Amazing article, straight in to my bookmarks! Thank you |
Beta Was this translation helpful? Give feedback.
-
Excellent article. I usually disable on tab change refreshing, but with you explanations get total sense to use it. |
Beta Was this translation helpful? Give feedback.
-
Brilliant article. Found two typos: - which means that evey time you e.g. mount a new
+ which means that every time you e.g. mount a new - passing Query Keys to setQueyDefaults follows the standard
+ passing Query Keys to setQueryDefaults follows the standard It seems that your Agree with the defaults being generally fine, specially the Thank you for your work! |
Beta Was this translation helpful? Give feedback.
-
This article is a must-read to everyone starting to use React Query! |
Beta Was this translation helpful? Give feedback.
-
Thanks for the great articles , I really enjoy and learn a lot from that. |
Beta Was this translation helpful? Give feedback.
-
Another great article in the series. |
Beta Was this translation helpful? Give feedback.
-
@larolldev react-query uses context under the hood only to distribute the QueryClient (with |
Beta Was this translation helpful? Give feedback.
-
@TkDodo thanks for your hint. With react-query's "async state management" in mind, I go further to use it to manage some states which needs to be stored in localStorage. It works well. The point is react-query cares only the Promise but doesn't care what was the data source.
|
Beta Was this translation helpful? Give feedback.
-
I also tried to use react-query to manage the authentication state:
So with react-query, now I can apply in my current project for 3 following use cases:
I'm going to integrate react-query with pusher for live updating. I think with above use cases I can solve my React state management easily in the cleanest way.
I removed all old redux codes, and feel like I got freedom again. :-) |
Beta Was this translation helpful? Give feedback.
-
@larolldev while it's true that react-query only cares about promises, I wouldn't use it for synchronous stuff - and reading from localStorage is synchronous. I've gone a bit into details on why that is in this github discussion: TanStack/query#489 (comment) |
Beta Was this translation helpful? Give feedback.
-
@AlcantaraRW sure, that's totally possible. Usually, I would advise to have filters done on the backend. So only active users would be part of the query key, then the backend would do the filtering. If you want to do frontend filtering, there are basically two ways:
|
Beta Was this translation helpful? Give feedback.
-
@TkDodo I fetch all unfiltered data when my page mounts, then when I click on my Filter component, inside the page, I would like to grab the data that React Query already cached to execute a filter on top of it and update the table. |
Beta Was this translation helpful? Give feedback.
-
@AlcantaraRW I don't quite follow. is the filtering now happening on the client side only, or is it happening on the server (you mentioned different fetch logic)? I think a codesandbox reproduction would be helpful to understand the situation better :) |
Beta Was this translation helpful? Give feedback.
-
Really helpful article |
Beta Was this translation helpful? Give feedback.
-
Great article @TkDodo. If you set |
Beta Was this translation helpful? Give feedback.
-
Awesome article, this is the first time I see your blog, it's 100% well done. |
Beta Was this translation helpful? Give feedback.
-
What is the best practice when you have nested components in routes? Is it still do stale time longer? |
Beta Was this translation helpful? Give feedback.
-
Hi , I have a [stupid ?] question. saving for example some config values in react query and allowing to share them between all the components. |
Beta Was this translation helpful? Give feedback.
-
@TkDodo thank you for the great article. |
Beta Was this translation helpful? Give feedback.
-
still tricky when processing pagination issues. |
Beta Was this translation helpful? Give feedback.
-
Would another tradeoff of using This seems especially cumbersome when you need a piece of fetched data in order to load any part of the app (like a User or a Session). I would need to handle the Loading state in every leaf component that uses the User query, but in reality no part of the app can load without the User being available and so all the loading states beneath the the application root won't get triggered. Fantastic article btw, I'm implementing RQ for the first time in my org so I'm trying to make sure I get the patterns right. |
Beta Was this translation helpful? Give feedback.
-
This is a great series. I'm also glad I found the |
Beta Was this translation helpful? Give feedback.
-
Thanks, great article! |
Beta Was this translation helpful? Give feedback.
-
I wonder if it's OK to sync react-query data into a Zustand store for just the same purpose you're using Context here. I have a rather complex page, with several queries I need to pass down the component tree, and a Zustand store, for keeping synchronous state on the page. It felt weird to use Context when I have Zustand (which is already great for avoiding props drilling), so I did something like this:
Now I can easily access this data in all child components, keeping all the react-query perks at the same time, with a slight taste of guilt from syncing state this way :) |
Beta Was this translation helpful? Give feedback.
-
This is a great article. I learnt a lot. Thank you! |
Beta Was this translation helpful? Give feedback.
-
React Query as a State Manager | TkDodo's blog
Everything you need to know to make React Query your single source of truth state manager for your async state
https://tkdodo.eu/blog/react-query-as-a-state-manager?utterances=59bd06b8dcfe487234bd5af58Q2m%2FkVpN9YKFH7h897nXK%2BW8vHzTHreZP59PuTGhgu7MlpG0XPLh49z6unbM5sEtiuOUwg5k2iaVTHKP9HPsN7Kh07WRKuGCCueuMOp0S4%2FS5PIdbpoUW2kaQE%3D
Beta Was this translation helpful? Give feedback.
All reactions