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

Bugfix: Offscreen instance is null during setState #24734

Merged
merged 2 commits into from
Jun 16, 2022

Commits on Jun 16, 2022

  1. [FORKED] Bugfix: Offscreen instance is null during setState

    During a setState, we traverse up the return path and check if any
    parent Offscreen components are currently hidden. To do that, we must
    access the Offscreen fiber's `stateNode` field.
    
    On a mounted Offscreen fiber, the `stateNode` is never null, so usually
    we don't need to refine the type. When a fiber is unmounted, though,
    we null out its `stateNode` field to prevent memory cycles. However,
    we also null out its `return` field, so I had assumed that an unmounted
    Offscreen fiber would never be reachable.
    
    What I didn't consider is that it's possible to call `setState` on a
    fiber that never finished mounting. Because it never mounted, it was
    never deleted. Because it was never deleted, its `return` field was
    never disconnected.
    
    This pattern is always accompanied by a warning but we still need to
    account for it. There may also be other patterns where an unmounted
    Offscreen instance is reachable, too.
    
    The discovery also suggests it may be better for memory
    usage if we don't attach the `return` pointer until the commit phase,
    though in order to do that we'd need some other way to track the return
    pointer during initial render, like on the stack.
    
    The fix is to add a null check before reading the instance
    during setState.
    acdlite committed Jun 16, 2022
    Configuration menu
    Copy the full SHA
    d410f0a View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    0a2937e View commit details
    Browse the repository at this point in the history