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 useLazyQuery forceUpdate loop regression. #7715

Merged
merged 3 commits into from
Feb 15, 2021
Merged

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Feb 15, 2021

PR #7655 changed this code so that the Promise.resolve().then(forceUpdate) branch was not taken when queryDataRef.current was falsy, so forceUpdate() was called immediately instead (the else branch).

As #7713 demonstrates, the Promise delay is important to avoid calling forceUpdate() synchronously during the initial render, which can lead to an infinite rendering loop.

To preserve the intent of #7655, we now do the queryDataRef.current check inside the Promise callback, since the component could have been unmounted before the callback fired.

Remaining work:

  • Investigate test failures in src/react/components/__tests__/client/Query.test.tsx

PR #7655 changed this code so that the Promise.resolve().then(forceUpdate)
branch was not taken when queryDataRef.current was falsy, so forceUpdate()
was called immediately instead (the else branch).

As #7713 demonstrates, the Promise delay is important to avoid calling
forceUpdate() synchronously during the initial render, which can lead to
an infinite rendering loop.

To preserve the intent of #7655, we now do the queryDataRef.current check
inside the Promise callback, since the component could have been unmounted
before the callback fired.
In some of our tests, onNewData is called between the first render/mount
and the useEffect callback that updates queryDataRef.current, so the logic
in onNewData that avoids calling forceUpdate if !queryDataRef.current may
prevent legitimate forceUpdate calls, if we check queryDataRef.current
before the useEffect callback has fired. By setting queryDataRef.current
immediately upon creating the new QueryData object, we eliminate this
brief window where the component has rendered but queryDataRef.current has
not yet been updated.
@benjamn benjamn self-assigned this Feb 15, 2021
@benjamn benjamn marked this pull request as ready for review February 15, 2021 19:51
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants