MCPcopy
hub / github.com/TanStack/query / replaceData

Function replaceData

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

Source from the content-addressed store, hash-verified

367}
368
369export function replaceData<
370 TData,
371 TOptions extends QueryOptions<any, any, any, any>,
372>(prevData: TData | undefined, data: TData, options: TOptions): TData {
373 if (typeof options.structuralSharing === 'function') {
374 return options.structuralSharing(prevData, data) as TData
375 } else if (options.structuralSharing !== false) {
376 if (process.env.NODE_ENV !== 'production') {
377 try {
378 return replaceEqualDeep(prevData, data)
379 } catch (error) {
380 console.error(
381 `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}`,
382 )
383
384 // Prevent the replaceEqualDeep from being called again down below.
385 throw error
386 }
387 }
388 // Structurally share data between prev and new data if needed
389 return replaceEqualDeep(prevData, data)
390 }
391 return data
392}
393
394export function keepPreviousData<T>(
395 previousData: T | undefined,

Callers 2

createResultMethod · 0.90
setDataMethod · 0.90

Calls 1

replaceEqualDeepFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…