Skip to content

Bug: effect cleanup cannot distinguish hidden Activity from unmount #38403

Backlog

Description

@xiaoxiyao

React version:

  • Affected React/React DOM version: to be verified
  • Reproduced with React Activity

Steps To Reproduce

  1. Place a component that owns a stateful external instance inside <Activity mode="hidden">.
  2. Initialize the external instance in an effect.
  3. Hide the Activity so that React cleans up the effect and its cleanup destroys the instance.
  4. Restore the Activity so that React reruns the effect and initializes a new instance.
  5. Observe loss of scroll position or other instance state.

Link to code example:

The current behavior

Activity hiding currently runs the effect cleanup for the subtree. Cleanup cannot distinguish temporary hiding from a true unmount, so libraries that destroy external instances in effect cleanup destroy stateful resources even though the Activity DOM and component state are preserved.

When the Activity becomes visible again, React reruns the effect and creates a new external instance. The previous scroll position and other instance state are lost.

The expected behavior

React should provide a supported way to distinguish temporary hiding of an Activity subtree from a real unmount, or otherwise provide a lifecycle mechanism that lets integrations preserve state and external resources during hiding.

Libraries should be able to keep or suspend external resources while hidden and perform final destruction only when the component is truly unmounted.

If React intentionally does not expose this distinction, guidance for the recommended cleanup and restoration pattern would be helpful.

Related issue: KingSora/OverlayScrollbars#770

Activity

  1. added
    Status: Unconfirmed
    on Nov 27, 2026
  2. sleitor commented on Nov 29, 2026

    @sleitor
    Contributor

    Feasibility note from looking into this: the behavior is intentional. <Activity mode="hidden"> unmounts effects (runs cleanup) while preserving state and DOM, so that hidden trees behave like unmounted ones for side effects. Exposing a way to tell hide from unmount would be a new public API or lifecycle (and would partly undermine that model), so it's a React-team design decision, not something an external PR can settle.

    In the meantime, the supported pattern is to make the effect setup/cleanup symmetric and idempotent: keep the external instance (and its state, such as scroll position) in a ref or in the library's own store, have cleanup only suspend or detach, and have setup restore it. Final destruction can be deferred, e.g. by scheduling it and cancelling it if the effect re-runs, or by tying it to the owning component's real lifetime through a stable cleanup. The guidance in the Activity docs about effects in hidden trees is also the place to document this if the team wants it. Not planning a PR here, to avoid a competing API proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: Unconfirmed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions