Skip to content

clear trackedProps of the pre render #2453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,8 @@ export class QueryObserver<
this.shouldNotifyListeners(this.currentResult, prevResult)
) {
defaultNotifyOptions.listeners = true
// clear `trackedProps` of the pre render
this.trackedProps = []
}

this.notify({ ...defaultNotifyOptions, ...notifyOptions })
Expand Down
38 changes: 38 additions & 0 deletions src/react/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,44 @@ describe('useQuery', () => {
expect(states[1]?.dataUpdatedAt).not.toBe(states[2]?.dataUpdatedAt)
})

it('should always clear the `trackedProps` after re-render', async () => {
const key = queryKey()
let renderCount = 0
let data = 'hello'
const states: UseQueryResult<string>[] = []

function Page() {
const state = useQuery(key, () => data, {
notifyOnChangeProps: 'tracked',
})

states.push(state)

React.useEffect(() => {
renderCount++

if (renderCount === 1) {
setActTimeout(() => {
data = 'hi'
state.refetch()
}, 5)
}
}, [state])

return (
<div>
<h1>{renderCount === 0 ? state.data : 'test'}</h1>
</div>
)
}

renderWithClient(queryClient, <Page />)

await sleep(30)
expect(renderCount).toBe(2)
expect(states.length).toBe(2)
})

it('should track properties and only re-render when a tracked property changes', async () => {
const key = queryKey()
const states: UseQueryResult<string>[] = []
Expand Down