Skip to content

Warning: Function components cannot be given refs in DropdownMenuTrigger #2947

@jiweiyuan

Description

@jiweiyuan

Bug Description

When using CopilotChat component, React throws a warning about function components not being able to receive refs:

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Check the render method of `Primitive.button.SlotClone`.
    at DropdownMenuTrigger
    at CopilotChatInput2.AddMenuButton

Steps to Reproduce

  1. Use <CopilotChat /> component in a React application
  2. Open browser console
  3. Warning appears on render

Expected Behavior

No warning should appear - all components passed to Radix UI's DropdownMenuTrigger should properly forward refs.

Actual Behavior

Warning is logged to console on every render of the CopilotChat component.

Environment

  • @copilotkit/react-core: v2 (latest)
  • React: 18.x
  • Browser: Chrome

Root Cause Analysis

The AddMenuButton component inside CopilotChatInput passes a function component to DropdownMenuTrigger that doesn't use React.forwardRef(). Radix UI's DropdownMenuTrigger expects its child to forward refs for accessibility and focus management.

Suggested Fix

Wrap the button component passed to DropdownMenuTrigger with React.forwardRef():

const TriggerButton = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => (
  <button ref={ref} {...props} />
));

Related Issues

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