T
TanStack4y ago
like-gold

Query parameter based on another useQuery

So, I have two useQuery calls, the first one gives me an id, I insert that same id into the second useQuery function so that it will get passed as a query parameter, however, since it's initially undefined, the request that fires has the query parameter as undefined, console.log also logs undefined first and then the correct value, how do I get around this?
const { data: users } = useQuery(["users"], () => getUsers(), {
select: (data) =>
data?.filter((user) => user.email === accountEmail).map(user => user.id),
});

const { isLoading, isError, data, error } = useQuery(["table"], () =>
tableData(users)
);
const { data: users } = useQuery(["users"], () => getUsers(), {
select: (data) =>
data?.filter((user) => user.email === accountEmail).map(user => user.id),
});

const { isLoading, isError, data, error } = useQuery(["table"], () =>
tableData(users)
);
1 Reply
genetic-orange
genetic-orange4y ago
seems like a standard dependent query where you'd disable the second query until you have all the necessary inputs? https://tanstack.com/query/v4/docs/guides/dependent-queries
Dependent Queries | TanStack Query Docs
Dependent (or serial) queries depend on previous ones to finish before they can execute. To achieve this, it's as easy as using the enabled option to tell a query when it is ready to run: `tsx

Did you find this page helpful?