blog/react-query-meets-react-router #84
Replies: 58 comments 161 replies
-
From this article can I suppose that the specific features you showed about react router are not going to be implemented on react location ? |
Beta Was this translation helpful? Give feedback.
-
Great article, love your blog and work. |
Beta Was this translation helpful? Give feedback.
-
As always, I love your work and it's much neater than my solution to the problem. One thing I ran into (and I don't see addressed here) is the errorComponent prop in react router. If the loader fails (or anything else in the route I think) then it displays the error component. In your example it looks like we'd need almost-duplicated code for the standard react-query error case in the component and then the react-router error component. Is there some clean way round that? |
Beta Was this translation helpful? Give feedback.
-
I love the content on your blog |
Beta Was this translation helpful? Give feedback.
-
What do I like about this approach in general, that we dropped hard dependency on React and mixing separation of concers like: hooks + API + Render phase |
Beta Was this translation helpful? Give feedback.
-
I am wondering, with this kind of setup, would you recommend using the route as the QueryKey? I think v3 supported that. Does v4 do that as well? |
Beta Was this translation helpful? Give feedback.
-
Great article! How would this work with Apollo, for example? Tricky to wrap my head around it 😅 |
Beta Was this translation helpful? Give feedback.
-
react router beta also have suspense and skeleton. Should I use of react-router or still implement by using react query |
Beta Was this translation helpful? Give feedback.
-
Great article. Heads up that the link to the tutorial in the first paragraph is a 404. It should link to https://beta.reactrouter.com/en/dev/start/tutorial instead. |
Beta Was this translation helpful? Give feedback.
-
Suppose in contactLoader the function take too slow, how do I show loading spinner in the route |
Beta Was this translation helpful? Give feedback.
-
Hey, I seem to be getting a TypeError with this approach. Any thoughts on how I might work around that? |
Beta Was this translation helpful? Give feedback.
-
Recently I wrote a library called react-query-kit that aims to create the query in a typesafe way and be simple to use.
|
Beta Was this translation helpful? Give feedback.
-
Suppose I have data saved in context api. So how can I get data context api in |
Beta Was this translation helpful? Give feedback.
-
I follow the above example and are seeing two fetch requests for the initial page load (subsequent request only fired once). Do you have any insight on how could this be solved? Thanks for the guide |
Beta Was this translation helpful? Give feedback.
-
Hello, I wonder what would |
Beta Was this translation helpful? Give feedback.
-
Just wondering. Thank you in advance. App.jsx
Landing.jsx
|
Beta Was this translation helpful? Give feedback.
-
How this pattern can work with I've tried refactoring some Similar, for some The end result was a bit problematic, with rerendering on first load (leaving some Even I've fixed somehow the first load and rerendering, later optimization with I really like to know more what is the best practice to leverage this pattern. |
Beta Was this translation helpful? Give feedback.
-
I want to use loaders to fetch data from server before component renders and each API call has Bearer token attached to it, I'm using axios interceptors to intercept each call and attach token with request heeders.. But when the call is initiated by a loader the token is not getting attached to it. it seems like loader get call much early even before the interceptors invoked. |
Beta Was this translation helpful? Give feedback.
-
Awesome article as always ! In your example of how you use initial data to get rid of the union "Contact | undefined" type for "data", I am wondering how you do that if you have a custom hook "useContact" ? Do we need to write an overload for this hook to have the same result as using "useQuery" directly ? |
Beta Was this translation helpful? Give feedback.
-
It's a very good technology. I'm using it well. However, this time, I encountered a problem.
I'm trying to use the However, even though I'm changing the query string using a button and updating the URL, if I don't perform a direct refresh or if I click outside of the page, and then return, the queryFn that depends on the URL doesn't work as expected. I tried using I've confirmed that the query string is changing and the component is re-rendering. I've also verified through console logs that it's accessing the loader accordingly. Additionally, the updated query string is being recognized within the loader function. However, the queryFunction doesn't seem to respond to the updated URL (API calls are not triggered). |
Beta Was this translation helpful? Give feedback.
-
If they are indeed "a match made in heaven", is it fair to expect there to be a sort of official collaboration through which we would have react-router coming with the benefits of react-query out-of-the-box? Maybe, an API where you don't have to explicitly pass a QueryClient to the loader? Also, some other improvements in terms of abstraction could be possible. For example, in my opinion, having to use useQuery() for accessing the data in the component is bad abstraction; being able to access through useLoaderData() makes more semantic sense. |
Beta Was this translation helpful? Give feedback.
-
@TkDodo thanks for this great guidelines. I have a question regarding types for queries with Is this example const initialData = useLoaderData() as Awaited<
ReturnType<ReturnType<typeof loader>>
>
const params = useParams()
const { data: contact } = useQuery({
...contactDetailQuery(params.contactId),
initialData,
}) basically equals to this? const params = useParams()
const { data: contact } = useQuery({
...contactDetailQuery(params.contactId),
}) as DefinedUseQueryResult<Contact> And if so, then maybe it is worth to write some custom typings to infer query const { data } = useContactQuery(); // data: Contact | undefined
const { data ) = useContactQuery<'defined'>() // data: Contact Invoking I ques, we can put Anyway, I would appreciate any tips and suggestion how to properly organize this stuff. |
Beta Was this translation helpful? Give feedback.
-
Great article. If we had multiple nested components having own queries on one page, and if we fetched all of these in the loader (with awaiting) it could slow down (delay) the initial route rendering. My idea is to just fire multiple prefetches in the loader to minimize the query waterfall. Will it work correctly? I mean will the nested useQuery now that there is already pending fetch for that query and it’s loading time will be shorter compared to case when it would start fetching after initial render of that child component? |
Beta Was this translation helpful? Give feedback.
-
This article is very wonderful. I just have this question. What about the loading part. If we use useQuery we have isPending, where we can use like making faded background on some component. When using ensureQuery, how can we achieve that (Thinking of using state (loading)). Also, just an additional question, what's the difference of using useQuery and ensureQuery |
Beta Was this translation helpful? Give feedback.
-
Typo here:
Should be |
Beta Was this translation helpful? Give feedback.
-
Reading this on 2024, what a great article even though it's almost 2 years old. Wondering if there is any improvement to be done now that react-query is on V5. Edit: seems like returning the query as an object did the trick
|
Beta Was this translation helpful? Give feedback.
-
Good stuff, I would love to use the best of both worlds. I would like to keep all the Seems to work but it is like I've set defer to await always, any advice would be much appreciated, thanks. |
Beta Was this translation helpful? Give feedback.
-
Thank you, great article! I'm not sure I follow why useLoaderData is needed; fetching by e.g. |
Beta Was this translation helpful? Give feedback.
-
So how about using dispatch inside action ? |
Beta Was this translation helpful? Give feedback.
-
It works great, thanks for the post! However, I'm curious about this part:
Don't you lose some granular error control with this? I'm currently grabbing |
Beta Was this translation helpful? Give feedback.
-
blog/react-query-meets-react-router
React Query and React Router are a match made in heaven.
https://tkdodo.eu/blog/react-query-meets-react-router
Beta Was this translation helpful? Give feedback.
All reactions