Skip to content

Bug: forwardRef provides an empty object as ref, causing can't define property "current" error. #26419

@hansottowirtz

Description

@hansottowirtz

React version: 18.3.0-next-3ba7add60-20221201

Hard to reproduce.

In Next.js, in rare cases, and only after a fast refresh, the ref object when using forwardRef is simply {}.

This leads to the TypeError: can't define property "current": Object is not extensible error when used with useImperativeHandle.

function Parent() {
  const carouselRef = useRef<any>();

  return <Suspense><Carousel ref={carouselRef} /></Suspense>
}

const Carousel = forwardRef((props, ref) => {
  if (!('current' in ref)) { debugger; }
  const thing = use(aPromise);
  return ...;
});

Screenshot 2023-03-17 at 18 49 21

In updateImperativeHandle, the ref is already an empty object.

Screenshot 2023-03-17 at 19 13 13

I haven't investigated it further yet, the stacktraces are quite hard to follow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions