-
Hello, I've recently played a little bit with react-query v5 and what caught my attention was the new useSuspenseQuery hook which looks promising. I've decided to use that solution to improve loaders in my app and reduce bundle size by combining API calls with code-splitting (I am using React without any framework). The useSuspenseQuery hook seems to be a great solution for my problem and it works fine for simple cases. However, I've noticed some limitations:
With useSuspenseQuery, this will trigger a fallback and a loader will be displayed for my entire page because I'm passing currentPage as a queryKey. As mentioned above, I don't want to trigger a fallback in such situation. This is my current workaround:
Now I can refetch my query without triggering a fallback. In order to display a loader inside my table, i am using isFetching property which seems to work for useSuspenseQuery. Do you guys think it's a valid approach? If so, maybe it could be a nice feature to consider adding as a prop?
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 14 replies
-
Thanks for the feedback 🙏. Let me try to address them one by one:
Dependent Queries work out of the box with suspense, because the component suspends in between 😅 . The example from the docs simply becomes:
This is actually one of the "pitfalls" with suspense - this creates a waterfall in requests, which is nice for dependent queries, but if you want multiple calls to queries to happen in parallel, you'd need
That's an architectural constraint because technically, suspense unmounts your component to render the fallback, then mounts it again when the promise resolves. This is expected imo if you set
While this is nice (if used correctly, see the aforementioned waterfall when calling
This is in the docs:
You want All in all, suspense is an architectural pattern shift and not just a flag that you turn on to get a "global loading spinner". It actually shines when used with a server side framework and streaming, not so much on the client only. This is also the reason why we've moved away from the |
Beta Was this translation helpful? Give feedback.
-
Hello @TkDodo , https://codesandbox.io/p/sandbox/laughing-ishizaka-ykhj5q?file=%2Fsrc%2Findex.jsx%3A38%2C44 When switching the page for the first time, loader is not displayed. After changing from page 1 to page 2 and then back to page 1, the loader is visible. I'm wondering if sticking with useSuspenseQuery is a good idea for my use case. I am aware that there are alternatives with useQuery such as keepPreviousData for pagination. However, I really like the idea of using Suspense instead of tracking loading state with isLoading property. What would you recommend? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
@TkDodo What if |
Beta Was this translation helpful? Give feedback.
-
Hey @TkDodo thanks for all the great information. I am also struggling with the removal of the My use case is to load the initial data of my application:
So the way I was expecting to do that was with the
In this example, my goal is to NOT make the session call if the route is not protected. How would you do this? Should it be handled outside of React Query? Thank you 🙏🏻 Patrick |
Beta Was this translation helpful? Give feedback.
-
The limitation of not being able to disable a useSuspenseQuery hook is also giving me trouble in my case: |
Beta Was this translation helpful? Give feedback.
Thanks for the feedback 🙏. Let me try to address them one by one:
Dependent Queries work out of the box with suspense, because the component suspends in between 😅 . The example from the docs simply becomes:
This is actually…