Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hansottowirtz opened this issue Mar 17, 2023 · 4 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@hansottowirtz
Copy link
Contributor

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.

@hansottowirtz hansottowirtz added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Mar 17, 2023
@joaoromeira
Copy link

You should try this approach (I think you want to use the ref to control the carousel)

import { useEffect } from 'react';
import { forwardRef, Suspense, useImperativeHandle, useRef } from 'react'


type CarouselProps = any

type CarouselHandle = {
  init: () => void
}

const Carousel  = forwardRef<CarouselHandle, CarouselProps>((props, ref) => {
  useImperativeHandle(ref, () => ({
   init: () => {
     alert('Init!')
   } 
  }))

  return <div>Carousel</div>
});


const Parent = () => {
  const carouselRef = useRef<CarouselHandle>(null);
  
  useEffect(() => {
    if(carouselRef.current)
      carouselRef.current.init()
  }, [carouselRef])
  return (
    <Suspense fallback={<div />}>
      <Carousel ref={carouselRef} />
    </Suspense>
  )
}

export default Parent

@eps1lon
Copy link
Collaborator

eps1lon commented Mar 29, 2023

Can you open this on the Next.js repo as well? They might have a better idea how to reduce this to a repro just using React.

@hansottowirtz
Copy link
Contributor Author

@eps1lon I have created a failing test/reproduction in #26420, I'm waiting for some help over there.

acdlite added a commit that referenced this issue Apr 2, 2023
Continuation of #26420

Fixes #26385 and
#26419

---------

Co-authored-by: eps1lon <silbermann.sebastian@gmail.com>
Co-authored-by: Andrew Clark <git@andrewclark.io>
github-actions bot pushed a commit that referenced this issue Apr 2, 2023
Continuation of #26420

Fixes #26385 and
#26419

---------

Co-authored-by: eps1lon <silbermann.sebastian@gmail.com>
Co-authored-by: Andrew Clark <git@andrewclark.io>

DiffTrain build for [7329ea8](7329ea8)
@hansottowirtz
Copy link
Contributor Author

Fixed by #26535

jerrydev0927 added a commit to jerrydev0927/react that referenced this issue Jan 5, 2024
Continuation of facebook/react#26420

Fixes facebook/react#26385 and
facebook/react#26419

---------

Co-authored-by: eps1lon <silbermann.sebastian@gmail.com>
Co-authored-by: Andrew Clark <git@andrewclark.io>

DiffTrain build for [7329ea81c154d40800e30104be40f050e8c2af3e](facebook/react@7329ea8)
EdisonVan pushed a commit to EdisonVan/react that referenced this issue Apr 15, 2024
Continuation of facebook#26420

Fixes facebook#26385 and
facebook#26419

---------

Co-authored-by: eps1lon <silbermann.sebastian@gmail.com>
Co-authored-by: Andrew Clark <git@andrewclark.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

3 participants