I've started playing with React-Query and it works great if I only need to fetch data from a single collection in my database. However, I'm struggling to find a good way to query multiple collections for use in a single component.
One Query (no problem):
const { isLoading, isError, data, error } = useQuery('stuff', fetchStuff)
if (isLoading) {
return <span>Loading...</span>
}
if (isError) {
return <span>Error: {error.message}</span>
}
return (
<ul>
{data.map(stuff => (
<li key={stuff.id}>{stuff.title}</li>
))}
</ul>
)
}
Multiple Queries to Different Collections ():
const { isLoading: isLoadingStuff, isError: isErrorStuff, data: stuff, error: errorStuff } = useQuery('stuff', fetchStuff);
const { isLoading: isLoadingThings, isError: isErrorThings, data: Things, error: errorThings } = useQuery('things', fetchThings);
const { isLoading: isLoadingDifferentStuff, isError: isErrorDifferentStuff, data: DifferentStuff, error: errorDifferentStuff } = useQuery('DifferentStuff', fetchDifferentStuff);
const isLoading = isLoadingStuff || isLoadingThings || isLoadingDifferentStuff
const isError = isErrorStuff || isErrorThings || isErrorDifferentStuff
const error = [errorStuff, errorThings, errorDifferentStuff]
if (isLoading) {
return <span>Loading...</span>
}
if (isError) {
return (
<span>
{error.forEach((e) => (e ? console.log(e) : null))}
Error: see console!
</span>
);
}
return (
<>
<ul>
{stuff.map((el) => (
<li key={el.id}>{el.title}</li>
))}
</ul>
<ul>
{things.map((el) => (
<li key={el.id}>{el.title}</li>
))}
</ul>
<ul>
{differentStuff.map((el) => (
<li key={el.id}>{el.title}</li>
))}
</ul>
</>
);
}
I'm sure there must be a better way to do this. I'm very interested in React-Query for multiple reasons but one nice benefit is to reduce boilerplate. However, this approach doesn't seem much better than using useEffect and useState to manage my api calls. I did find the useQueries hook but it didn't make this any cleaner really.
Does anyone know if there is a way in React-Query to make multiple queries and only get back one isLoading, isError, and error(array?) response? Or just a better way to handle multiple queries that I'm missing?