Skip to content

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

Closed

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.

Activity

  1. added this to the 0.5.1 milestone on Aug 29, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions