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

[Fresh] Don't ignore dependencies for render phase updates #16574

Merged
merged 1 commit into from
Aug 28, 2019

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Aug 26, 2019

During a Fresh update, we ignore dependencies. We let the effects re-run, even if deps are [], and free the memo'd values.

This is important because it prevents us from referencing stale code. For example, if we edit top-level computeExpensive, we still want useMemo(() => computeExpensive(), []) to re-evaluate.

Sometimes this may cause problems, but arguably this points to other flaws in components or Hooks that should be fixed regardless.


There is a failure case though. Generally, while components shouldn’t rely on useMemo for semantics, there is still an expectation that memo chains can “settle”. For example, useSubscription does a render-phase update if its arguments differ from its state:

if (
state.getCurrentValue !== getCurrentValue ||
state.subscribe !== subscribe
) {

It has to rely on something like useMemo in the calling code to keep the source eventually stable.


The problem with our current approach is that during a Fresh update, dependencies are ignored completely. So even with useMemo, things never truly “settle”. This breaks the pattern relied upon by useSubscription, and likely, other similar mechanisms.

The fix is to only ignore dependencies during the first render after the patch. By that point it has run, all memo'd values have been recreated with fresh code. So it makes sense that we can turn off the cache busting flag for all subsequent render-phase updates of this component, thus letting it settle. This fixes the infinite loop in the regression test I've added.

@sizebot
Copy link

sizebot commented Aug 26, 2019

No significant bundle size changes to report.

Generated by 🚫 dangerJS

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice explanation. Good test.

@gaearon gaearon merged commit 01fb68b into facebook:master Aug 28, 2019
gaearon added a commit to gaearon/react that referenced this pull request Sep 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants