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

Method setQueryData

packages/query-core/src/queryClient.ts:286–319  ·  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,
      NoInfer<TInferredQueryFnData> | undefined
    >,
    options?: SetDataOptions,
  )

Source from the content-addressed store, hash-verified

284 * ```
285 */
286 setQueryData<
287 TQueryFnData = unknown,
288 TTaggedQueryKey extends QueryKey = QueryKey,
289 TInferredQueryFnData = InferDataFromTag<TQueryFnData, TTaggedQueryKey>,
290 >(
291 queryKey: TTaggedQueryKey,
292 updater: Updater<
293 InferDataFromTag<NoInfer<TQueryFnData>, TTaggedQueryKey> | undefined,
294 NoInfer<TInferredQueryFnData> | undefined
295 >,
296 options?: SetDataOptions,
297 ): NoInfer<TInferredQueryFnData> | undefined {
298 const defaultedOptions = this.defaultQueryOptions<
299 any,
300 any,
301 unknown,
302 any,
303 QueryKey
304 >({ queryKey })
305
306 const query = this.#queryCache.get<
307 InferDataFromTag<NoInfer<TQueryFnData>, TTaggedQueryKey>
308 >(defaultedOptions.queryHash)
309 const prevData = query?.state.data
310 const data = functionalUpdate(updater, prevData)
311
312 if (data === undefined) {
313 return undefined
314 }
315
316 return this.#queryCache
317 .build(this, defaultedOptions)
318 .setData(data, { ...options, manual: true })
319 }
320
321 /**
322 * 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