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

Function replaceData

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

Source from the content-addressed store, hash-verified

400}
401
402export function replaceData<
403 TData,
404 TOptions extends QueryOptions<any, any, any, any>,
405>(prevData: TData | undefined, data: TData, options: TOptions): TData {
406 if (typeof options.structuralSharing === 'function') {
407 return options.structuralSharing(prevData, data) as TData
408 } else if (options.structuralSharing !== false) {
409 if (process.env.NODE_ENV !== 'production') {
410 try {
411 return replaceEqualDeep(prevData, data)
412 } catch (error) {
413 console.error(
414 `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}`,
415 )
416
417 // Prevent the replaceEqualDeep from being called again down below.
418 throw error
419 }
420 }
421 // Structurally share data between prev and new data if needed
422 return replaceEqualDeep(prevData, data)
423 }
424 return data
425}
426
427export function keepPreviousData<T>(
428 previousData: T | undefined,

Callers 2

createResultMethod · 0.90
setDataMethod · 0.90

Calls 1

replaceEqualDeepFunction · 0.85

Tested by

no test coverage detected