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

Function replaceData

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

Source from the content-addressed store, hash-verified

544 * Outside production, it is also logged to the console.
545 */
546export function replaceData<
547 TData,
548 TOptions extends QueryOptions<any, any, any, any>,
549>(prevData: TData | undefined, data: TData, options: TOptions): TData {
550 if (typeof options.structuralSharing === 'function') {
551 return options.structuralSharing(prevData, data) as TData
552 } else if (options.structuralSharing !== false) {
553 if (process.env.NODE_ENV !== 'production') {
554 try {
555 return replaceEqualDeep(prevData, data)
556 } catch (error) {
557 console.error(
558 `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}`,
559 )
560
561 // Prevent the replaceEqualDeep from being called again down below.
562 throw error
563 }
564 }
565 // Structurally share data between prev and new data if needed
566 return replaceEqualDeep(prevData, data)
567 }
568 return data
569}
570
571/**
572 * 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