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

Function replaceData

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

Source from the content-addressed store, hash-verified

458}
459
460export function replaceData<
461 TData,
462 TOptions extends QueryOptions<any, any, any, any>,
463>(prevData: TData | undefined, data: TData, options: TOptions): TData {
464 if (typeof options.structuralSharing === 'function') {
465 return options.structuralSharing(prevData, data) as TData
466 } else if (options.structuralSharing !== false) {
467 if (process.env.NODE_ENV !== 'production') {
468 try {
469 return replaceEqualDeep(prevData, data)
470 } catch (error) {
471 console.error(
472 `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}`,
473 )
474
475 // Prevent the replaceEqualDeep from being called again down below.
476 throw error
477 }
478 }
479 // Structurally share data between prev and new data if needed
480 return replaceEqualDeep(prevData, data)
481 }
482 return data
483}
484
485/**
486 * Intended to be passed as a query's `placeholderData` option, for example

Callers 3

createResultMethod · 0.90
setDataMethod · 0.90
utils.test-d.tsxFile · 0.90

Calls 1

replaceEqualDeepFunction · 0.85

Tested by

no test coverage detected