Prevent query from running after query key changed #5808
-
Let's say my app uses the route: On mount, I have to perform two requests simultaneously:
So, request (1) fires off, and at the same time request (2) fires off using the Code looks something like this: const {jobId, stageSlug} = params;
const {data: job} = useJob(jobId);
const stages = job?.stages || [];
const stage = stages.find(stage => stage.slug === stageSlug);
const stageSlugOrStageId = stage?.id !== undefined ? {stageId: stage.id} : {stageSlug}
const query = useRequest2Query({jobId, stageSlugOrStageId}) // both params are passed to the query key and the query fn. However, while request (2) is on fligh, request (1) resolves and now I have access to the job's stages. const stageSlugOrStageId = {stageId: stage.id} and it will be passed to the query, and since the query key changed it will cause the previous request to be cancelled and perform a new request, but this time with the As you can understand this introduces a delay and multiple requests, which is not good. So my question is: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
sounds like you want to disable the second query until the first query has finished ? https://tanstack.com/query/v4/docs/react/guides/dependent-queries |
Beta Was this translation helpful? Give feedback.
interesting. what you can do is:
queryClient.getQueryData
on thejob
querythat way, you would only have one query, which always has the
slug
in the QueryKey, and you potentially "translate" it to an id right before the request.