Skip to content
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

Unexpected re-renders, notifyOnChangeProps question #4570

Closed
sanvyx opened this issue Nov 29, 2022 · 2 comments
Closed

Unexpected re-renders, notifyOnChangeProps question #4570

sanvyx opened this issue Nov 29, 2022 · 2 comments
Labels
duplicate This issue or pull request already exists

Comments

@sanvyx
Copy link

sanvyx commented Nov 29, 2022

Describe the bug

Hi, i have a question about notifyOnChangeProps. Docs says that the default behavior is tracked, but I see some unexpected re-renders and I'm trying to understand why they are happening.

Please take a look at this example:
https://codesandbox.io/s/quizzical-montalcini-jm2dyf?file=/src/index.js

I expect to see 2 renders: first when data is undefined and second when the data comes from the queryFn. Somehow i see 3 renders and i can't understand why.

Your minimal, reproducible example

https://codesandbox.io/s/quizzical-montalcini-jm2dyf?file=/src/index.js

Steps to reproduce

Go to the minimal reproducible example, refresh the page and check console logs.

Expected behavior

I expect to see 2 renders: first when data is undefined and second when the data comes from the queryFn. Somehow i see 3 renders and i can't understand why.

How often does this bug happen?

Every time

Screenshots or Videos

image

Platform

  • macOS Ventura
  • Chrome Version 107.0.5304.110 (Official Build) (x86_64)

Tanstack Query version

^4.16.1

TypeScript version

^4.6.4

Additional context

No response

@TkDodo
Copy link
Collaborator

TkDodo commented Nov 29, 2022

I added a log statement in the queryObserver, when renders are initiated by react-query, and it shows the following output:

render items:  undefined
queryFn
notify listeners QueryObserver
render items:  [{…}]
queryFn
render items:  [{…}]

as we can see, there is only one observer-triggered re-render happening - when data goes from undefined to an array.

also, if we move the logging to a useEffect, we can see:

queryFn
render items:  undefined
queryFn
render items:  [{…}]
render items:  [{…}]

so it really doesn't seem like the last render with items is triggered by the queryFn being run again.

interesting observation: I downgraded react to v17, and there, the log output is:

render items:  undefined
queryFn
render items:  [{…}]
queryFn

I think this might be related to:

and all the upstream issues about react18 and batching with useSyncExternalStore that are linked there. I think the react team has already agreed that this needs addressing on their end. I don't see much we can do here for now.

@TkDodo
Copy link
Collaborator

TkDodo commented Dec 14, 2022

I'll close this as a duplicate of #3772, we can track progress there.

@TkDodo TkDodo closed this as completed Dec 14, 2022
@TkDodo TkDodo added the duplicate This issue or pull request already exists label Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants