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

Add "nested-update" phase to Profiler API #20163

Merged
merged 1 commit into from
Nov 10, 2020

Commits on Nov 5, 2020

  1. Add "nested-update" phase to Profiler API

    Background:
    State updates that are scheduled in a layout effect (useLayoutEffect or componentDidMount / componentDidUpdate) get processed synchronously by React before it yields to the browser to paint. This is done so that components can adjust their layout (e.g. position and size a tooltip) without any visible shifting being seen by users. This type of update is often called a "nested update" or a "cascading update".
    
    Because they delay paint, nested updates are considered expensive and should be avoided when possible. For example, effects that do not impact layout (e.g. adding event handlers, logging impressions) can be safely deferred to the passive effect phase by using useEffect instead.
    
    This PR updates the Profiler API to explicitly flag nested updates so they can be monitored for and avoided when possible.
    
    Implementation:
    I considered a few approaches for this.
    
    Add a new callback (e.g. onNestedUpdateScheduled) to the Profiler that gets called when a nested updates gets scheduled.
    Add an additional boolean parameter to the end of existing callbacks (e.g. wasNestedUpdate).
    Update the phase param to add an additional variant: "mount", "update", or "nested-update" (new).
    I think the third option makes for the best API so that's what I've implemented in this PR.
    
    Because the Profiler API is stable, this change will need to remain behind a feature flag until v18. I've turned the feature flag on for Facebook builds though after confirming that Web Speed does not currently make use of the phase parameter.
    
    Quirks:
    One quirk about the implementation I've chosen is that errors thrown during the layout phase are also reported as nested updates. I believe this is appropriate since these errors get processed synchronously and block paint. Errors thrown during render or from within passive effects are not affected by this change.
    Brian Vaughn committed Nov 5, 2020
    Configuration menu
    Copy the full SHA
    01c6228 View commit details
    Browse the repository at this point in the history