Prevent useSuspenseQuery throw
Hi all.
The thing is that I have a suspense query that get's an array which I use to build up a navigation.
I've got static navigation items and with the suspense query I check if dynamic children should be added.
The thing is if the suspense query fails then it throws and I can't do the rendering of the app since it's the apps main navigation and pretty much at the top in rendering.
Is there a way to prevent the throw ( if the API for the query is not available ) and render the navigation without the children?
Any info would help.
Reading the docs I haven't found the info on a possibility.
3 Replies
fascinating-indigo•4w ago
useSuspenseQuery expects to have data defined, that's why it suspends and throws if request fails. You can change the queryFn to avoid the throw, potentially returning an empty array yourselfcorrect-apricotOP•4w ago
Hi @ferretwithabéret just as you've written I've chosen to use useQuery instead - and check for data and isSuccess to do the conditional merging of the children into the array 😄
Oh and basically I would update the queryFn with an empty return to get around the throw... Got it. Good tip 😉
pleasant-yellow•4w ago
yeah try/catch in the queryFn and return null or empty array