TanStackT
TanStack2y ago
30 replies
wispy-olive

Make another fetch call if the first one returns no data.

I'm making a fetch call to out backend API with data of the location of the user. Either 1. We get results and show them to the user "Here are some results within your area" or 2. There are no results within the location the user specified.

If 2, there are no results, but the fetch was successful. We want to make another fetch call, this time with specified location data to show the user "There was no results within your specified area, but here is some results outside of your area"

How do we do this in the best practice way with react-query?

Do I have to make two calls and have my own statehooks and useEffect, and then have different renders? I just wanna render DATA, regardless if it is from fetch call 1, or fetch call 2. And them maybe just have a boolean sayuing if it was fetch 1 or fetch 2.

I am using useInfiniteQuery btw, but I'm sure it doesn't matter.
Was this page helpful?