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

Function replaceData

packages/query-core/src/utils.ts:393–416  ·  view source on GitHub ↗
(prevData: TData | undefined, data: TData, options: TOptions)

Source from the content-addressed store, hash-verified

391}
392
393export function replaceData<
394 TData,
395 TOptions extends QueryOptions<any, any, any, any>,
396>(prevData: TData | undefined, data: TData, options: TOptions): TData {
397 if (typeof options.structuralSharing === 'function') {
398 return options.structuralSharing(prevData, data) as TData
399 } else if (options.structuralSharing !== false) {
400 if (process.env.NODE_ENV !== 'production') {
401 try {
402 return replaceEqualDeep(prevData, data)
403 } catch (error) {
404 console.error(
405 `Structural sharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${options.queryHash}]: ${error}`,
406 )
407
408 // Prevent the replaceEqualDeep from being called again down below.
409 throw error
410 }
411 }
412 // Structurally share data between prev and new data if needed
413 return replaceEqualDeep(prevData, data)
414 }
415 return data
416}
417
418export function keepPreviousData<T>(
419 previousData: T | undefined,

Callers 2

createResultMethod · 0.90
setDataMethod · 0.90

Calls 1

replaceEqualDeepFunction · 0.85

Tested by

no test coverage detected