T
TanStack•3y ago
extended-salmon

What is the best way to refetch with dependent queries

is there a way to also refetch the dependent query? currently, when i refetch for example the
userQuery
userQuery
id does not refetch the projects.
const { data: user, refetch } = useQuery({
queryKey: ['user', email],
queryFn: getUserByEmail,
})

const userId = user?.id

const {
status,
fetchStatus,
data: projects,
} = useQuery({
queryKey: ['projects', userId],
queryFn: getProjectsByUser,
enabled: !!userId,
})
const { data: user, refetch } = useQuery({
queryKey: ['user', email],
queryFn: getUserByEmail,
})

const userId = user?.id

const {
status,
fetchStatus,
data: projects,
} = useQuery({
queryKey: ['projects', userId],
queryFn: getProjectsByUser,
enabled: !!userId,
})
3 Replies
ratty-blush
ratty-blush•3y ago
structure the keys hierarchically:
queryKey: ['user', email],
queryKey: ['user', userId, 'projects'],
queryKey: ['user', email],
queryKey: ['user', userId, 'projects'],
then, you can use queryClient.invalidateQueries({ queryKey: ['user'] }) to target both of them
extended-salmon
extended-salmonOP•3y ago
@TkDodo 🔮 i dont need to call the refetch()?
ratty-blush
ratty-blush•3y ago
no

Did you find this page helpful?