-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Labels
Status: Unconfirmed
A potential issue that we haven't yet confirmed as a bug
Comments
hansottowirtz
added
the
Status: Unconfirmed
A potential issue that we haven't yet confirmed as a bug
label
Mar 17, 2023
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 |
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. |
acdlite
added a commit
that referenced
this issue
Apr 2, 2023
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
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 withuseImperativeHandle
.In
updateImperativeHandle
, the ref is already an empty object.I haven't investigated it further yet, the stacktraces are quite hard to follow.
The text was updated successfully, but these errors were encountered: