Skip to content

[Bug] useLayer recreates trigger refs and API objects on every render #5271

Description

@nynexman4464

Problem

useLayer({mode: 'context'}) creates a new trigger callback ref on every render. React therefore detaches and reattaches an unchanged trigger element, which removes and reapplies its CSS anchor name.

useLayer also returns a new API object on every render in both context and fixed modes. Consumers that correctly depend on the layer object rerun effects and reinstall listeners even when no layer state changed.

Examples in core include Carousel, ContextMenu, Popover, Tooltip, HoverCard, and BaseTypeahead.

Proposed fix

  • wrap the context trigger ref in useCallback
  • memoize the context and fixed return objects from their exposed members
  • preserve the existing rule that the result changes when an exposed member changes, including isOpen, callbacks, or render behavior

Regression coverage

Tests verify that:

  • the context trigger ref remains stable across unrelated rerenders
  • context and fixed return objects remain stable while their members are unchanged
  • changing onShow refreshes the API object but does not replace the trigger ref

No public types or behavior change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions