admin管理员组文章数量:1429079
I'm storing a reference to an image item using: const renderedImageRef = useRef()
. The ref is then assigned in render()
function using:
<img ref={renderedImageRef} src=... />
In another JSX item below, I try to access renderedImageRef.current.clientHeight
using:
<div style={{top:`${renderedImageRef.current.clientHeight}px`}}>
Hello world
</div>
But this produces an error in the console:
Uncaught TypeError: Cannot read properties of undefined (reading 'clientHeight')
Strangely enough, if I try to access renderedImageRef.current.clientHeight
from inside a useEffect
hook, it displays the height correctly:
useEffect(() => {
if(renderedImageRef !== null) {
console.log(renderedImageRef)
}
}, [renderedImageRef])
Why am I getting the console error?
I'm storing a reference to an image item using: const renderedImageRef = useRef()
. The ref is then assigned in render()
function using:
<img ref={renderedImageRef} src=... />
In another JSX item below, I try to access renderedImageRef.current.clientHeight
using:
<div style={{top:`${renderedImageRef.current.clientHeight}px`}}>
Hello world
</div>
But this produces an error in the console:
Uncaught TypeError: Cannot read properties of undefined (reading 'clientHeight')
Strangely enough, if I try to access renderedImageRef.current.clientHeight
from inside a useEffect
hook, it displays the height correctly:
useEffect(() => {
if(renderedImageRef !== null) {
console.log(renderedImageRef)
}
}, [renderedImageRef])
Why am I getting the console error?
Share Improve this question edited Mar 19, 2023 at 16:15 Youssouf Oumar 46.7k16 gold badges103 silver badges105 bronze badges asked Mar 12, 2022 at 11:19 SamSam 1,23021 silver badges48 bronze badges2 Answers
Reset to default 5A possible answer is that this line:
<div style={{top:`${renderedImageRef.current.clientHeight}px`}}>Hello world</div>
is ing in your code before this one:
<img ref={renderedImageRef} src=... />
In which case, it is normal that you are getting the error. It is to know that when you call const renderedImageRef = useRef()
, the value of renderedImageRef
is {current:undefined}
. The JSX must be rendered before a ref
gets its value in the current
field.
A solution is to use a state for the top
:
const [top, setTop] = useState(0);
useEffect(() => {
// At this point, the current is defined. This if is useful in case you use TypeScript
if (renderedImageRef.current) {
setTop(renderedImageRef.current.clientHeight);
}
}, []);
<div style={{top:`${top}px`}}>Hello world</div>
If you are using Next.JS, I think you have to change the dependency to renderedImageRef.current. So something like this:
const [top, setTop]=useState(0)
useEffect(() => {
if(renderedImageRef.current) {
setTop(renderedImageRef.current.clientHeight);
}
}, [renderedImageRef.current])
本文标签:
版权声明:本文标题:javascript - React, useRef not allowing to access current properties, getting Uncaught TypeError: Cannot read properties of unde 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745535479a2662251.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论