TanStackT
TanStack12mo ago
2 replies
correct-teal

Partial matching with setQueryData

I'm using the following query key in my project like this:
export const evaluationKeys = {
  all: ["evaluations"] as const,

  contents: (contentId: string, filters?: string) =>
    [
      ...evaluationKeys.all,
      "contents",
      contentId,
      ...(filters ? [{ filters }] : []),
    ] as const,
};

When I try to update the query cache using setQueryData, like this:
queryClient.setQueryData<EvaluationPayload[]>(
  evaluationKeys.contents(contentId),
  (currentEvaluationCache) => {
    if (!currentEvaluationCache) return [];
    return currentEvaluationCache.map((evaluation) => {
      if (evaluation.id === evaluationId) {
        return { ...evaluation, ...payload };
      }
      return evaluation;
    });
  }
);

It doesn't work as expected. Can we update cached data using a partial query key, or does setQueryData require an exact match? If so, how can I fix this issue?
Was this page helpful?