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

Fix re-rendering when another component reuses the same query #3195

Conversation

nathan-knight
Copy link
Contributor

Summary

When component A uses useQuery and has already loaded a result and then component B mounts and uses the same query, it currently causes component A to re-render because operation is not shallowly equal even though nothing about the result is changing.

This is a performance issue because it causes unnecessary re-renders.

It also can cause an infinite render loop with lazily loaded components because it can trigger a state update further up the tree during the initial render of the lazily loaded component which makes React back out of the render (which happened in my application, although I've had trouble creating a minimal reproduction). This issue is resolved with this change in my application.

Set of changes

  • Added a special case to isShallowDifferent for operation where it checks the operation key if the operations are not equal
  • Fixed a typo in CONTRIBUTING.md

packages/react-urql/src/hooks/state.ts Outdated Show resolved Hide resolved
.changeset/fifty-sheep-battle.md Outdated Show resolved Hide resolved
nathan-knight and others added 2 commits April 28, 2023 11:01
@kitten kitten merged commit b665828 into urql-graphql:main Apr 28, 2023
@github-actions github-actions bot mentioned this pull request Apr 28, 2023
@nathan-knight nathan-knight deleted the fix/prevent-state-update-with-same-operation branch April 28, 2023 15:35
@github-actions github-actions bot mentioned this pull request Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants