`suspense: true` is not triggering Suspense
Hello,
In my codebase I have the following code:
However it throws
Suspense not called on useUser. I thought the way that suspense worked is that useQuery itself would throw an object that would be caught by the parent Suspense component and would not continue to the next line until the request is completed. Do I misunderstand?11 Replies
absent-sapphireOP•3y ago
I have a parent
<Suspense but not <ErrorBoundary, not sure if I need one
More context:
error message:
More error message:
And console.logging the query before the throw
So it definitely should be calling parent Suspense right? Unless I misunderstand how it worksxenial-black•3y ago
fetchStatus is idle so it's not suspending. Are you using enabled or a persist plugin?
absent-sapphireOP•3y ago
I'm using persist
So looks like this is why: https://github.com/TanStack/query/blob/512d059a03f85289edf7e56bccc1ce5f050b5c90/packages/react-query/src/suspense.ts
Making this change at the top level seems to fix it (SuspenseFallback is just my generic loading page), is this the right way to go about it?
xenial-black•3y ago
It's up to you what to display in this case. We don't put the query in loading state because after the restore from persistence is done, the query might have fresh data which doesn't require a loading state
absent-sapphireOP•3y ago
I mean i just want to not render my app until the data is fetched from local storage, this seems to be the best way to do that as far as I can see.
This works fine, but as a thought experiment, what do you consider the state when persistence is loading to be? I would consider that to be loading (trigger suspense) since react-query probably can't do anything until that is done
xenial-black•3y ago
You can read via
useIsRestoring and choose to not render your app until we're done, that's fine. We don't do that per default because it would ruin SSRxenial-black•3y ago
persistQueryClient | TanStack Query Docs
This is set of utilities for interacting with "persisters" which save your queryClient for later use. Different persisters can be used to store your client and cache to many different storage layers.
Build Persisters
xenial-black•3y ago
It's in status:loading and fetchStatus:idle. Since the queryFn is not running, we're not suspending.
absent-sapphireOP•3y ago
Fair enough, my intuition was initially that the
isLoading is what triggers a suspense as opposed to fetching. Here is a trickier issue I'm coming across that I can't seem to put my finger on.
So when my device is connected to the internet my (other) code works, however when my device is offline, I get the following error: Uncaught Error: Suspense not called for storage.dashboards
This is after I added the onSuccess clause above. Here is the following code:
What is odd is that it works when connected to the internet, but not when it's offline. Maybe it has to do with the meta.storable code (copied from docs). I think the onSuccess callback code above stops this from rendering until the cache is loaded from localStorage but maybe react-query detects that the network is offline and it's not available in the storage (because of storable: false). Not sure if theres a way for me to tell react query to try this query even when the network is offline since it doesnt actually make a network request. I do not see a "Fetching data from storage" log anywherexenial-black•3y ago
Set
networkMode:'always'absent-sapphireOP•3y ago
Thanks! Sorry for bugging you with so many simple questions