MCPcopy Create free account
hub / github.com/TanStack/query / setQueryData

Method setQueryData

packages/query-core/src/queryClient.ts:290–323  ·  view source on GitHub ↗

* Synchronous way to immediately update a query's cached data. If the updater (or the value * passed) resolves to `undefined`, the cache is left untouched and no query is created; * otherwise, if the query does not exist yet, it will be created. To update multiple queries * at once by parti

(
    queryKey: TTaggedQueryKey,
    updater: Updater<
      InferDataFromTag<NoInfer<TQueryFnData>, TTaggedQueryKey> | undefined,
      DistributiveNoInfer<TInferredQueryFnData> | undefined
    >,
    options?: SetDataOptions,
  )

Source from the content-addressed store, hash-verified

288 * ```
289 */
290 setQueryData<
291 TQueryFnData = unknown,
292 TTaggedQueryKey extends QueryKey = QueryKey,
293 TInferredQueryFnData = InferDataFromTag<TQueryFnData, TTaggedQueryKey>,
294 >(
295 queryKey: TTaggedQueryKey,
296 updater: Updater<
297 InferDataFromTag<NoInfer<TQueryFnData>, TTaggedQueryKey> | undefined,
298 DistributiveNoInfer<TInferredQueryFnData> | undefined
299 >,
300 options?: SetDataOptions,
301 ): NoInfer<TInferredQueryFnData> | undefined {
302 const defaultedOptions = this.defaultQueryOptions<
303 any,
304 any,
305 unknown,
306 any,
307 QueryKey
308 >({ queryKey })
309
310 const query = this.#queryCache.get<
311 InferDataFromTag<NoInfer<TQueryFnData>, TTaggedQueryKey>
312 >(defaultedOptions.queryHash)
313 const prevData = query?.state.data
314 const data = functionalUpdate(updater, prevData)
315
316 if (data === undefined) {
317 return undefined
318 }
319
320 return this.#queryCache
321 .build(this, defaultedOptions)
322 .setData(data, { ...options, manual: true })
323 }
324
325 /**
326 * Synchronous way to immediately update the cached data of multiple queries at once, using

Callers 15

setQueriesDataMethod · 0.95
ExamplesFunction · 0.45
ExamplesFunction · 0.45
ExamplesFunction · 0.45
ExamplesFunction · 0.45
ExamplesFunction · 0.45
ExamplesFunction · 0.45
ClearArrayButtonFunction · 0.45
DeleteItemButtonFunction · 0.45
ToggleValueButtonFunction · 0.45
ExplorerFunction · 0.45
Explorer.test.tsxFile · 0.45

Calls 5

defaultQueryOptionsMethod · 0.95
functionalUpdateFunction · 0.90
getMethod · 0.80
setDataMethod · 0.80
buildMethod · 0.45

Tested by 4

seedQueriesFunction · 0.36
createQueryFunction · 0.36
callbackTestFunction · 0.36
callbackTest2Function · 0.36