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

Method setQueryData

packages/query-core/src/queryClient.ts:273–306  ·  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

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