Skip to content

Commit 9495006

Browse files
authored
fix(queryObserver): always structurally share placeholderData (#4402)
1 parent fc98d31 commit 9495006

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

packages/query-core/src/queryObserver.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,6 @@ export class QueryObserver<
517517
if (options.select && typeof placeholderData !== 'undefined') {
518518
try {
519519
placeholderData = options.select(placeholderData)
520-
placeholderData = replaceData(
521-
prevResult?.data,
522-
placeholderData,
523-
options,
524-
)
525520
this.selectError = null
526521
} catch (selectError) {
527522
if (process.env.NODE_ENV !== 'production') {
@@ -534,7 +529,7 @@ export class QueryObserver<
534529

535530
if (typeof placeholderData !== 'undefined') {
536531
status = 'success'
537-
data = placeholderData as TData
532+
data = replaceData(prevResult?.data, placeholderData, options) as TData
538533
isPlaceholderData = true
539534
}
540535
}

packages/query-core/src/tests/queryObserver.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,24 @@ describe('queryObserver', () => {
505505
expect(results[1]).toMatchObject({ status: 'success', data: 'data' })
506506
})
507507

508+
test('should structurally share placeholder data', async () => {
509+
const key = queryKey()
510+
const observer = new QueryObserver(queryClient, {
511+
queryKey: key,
512+
enabled: false,
513+
queryFn: () => 'data',
514+
placeholderData: {},
515+
})
516+
517+
const firstData = observer.getCurrentResult().data
518+
519+
observer.setOptions({ placeholderData: {} })
520+
521+
const secondData = observer.getCurrentResult().data
522+
523+
expect(firstData).toBe(secondData)
524+
})
525+
508526
test('the retrier should not throw an error when reject if the retrier is already resolved', async () => {
509527
const key = queryKey()
510528
let count = 0

0 commit comments

Comments
 (0)