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

Method setQueryData

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

Source from the content-addressed store, hash-verified

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

createQueryFunction · 0.36
callbackTestFunction · 0.36
callbackTest2Function · 0.36