admin管理员组文章数量:1406052
This is the part of my react application, where I've used a ref.current which is equal to null, then I'm changing it's property, I know like this is surely gonna give me error but in one of my friend's project with same react ponent WorkPage it's working fine. I'm so confused, is there any sort of fault in mine?
const WorkPage = () => {
const ref = useRef(null);
useEffect(() => {
let element = ref.current;
const rotate = () => {
element.style.transform = `translateX(${-window.pageYOffset}px)`
};
window.addEventListener('scroll', rotate)
return () => window.removeEventListener('scroll', rotate)
}, [])
This is the part of my react application, where I've used a ref.current which is equal to null, then I'm changing it's property, I know like this is surely gonna give me error but in one of my friend's project with same react ponent WorkPage it's working fine. I'm so confused, is there any sort of fault in mine?
const WorkPage = () => {
const ref = useRef(null);
useEffect(() => {
let element = ref.current;
const rotate = () => {
element.style.transform = `translateX(${-window.pageYOffset}px)`
};
window.addEventListener('scroll', rotate)
return () => window.removeEventListener('scroll', rotate)
}, [])
Share
Improve this question
asked Jan 18, 2022 at 9:46
Gourav Singh RawatGourav Singh Rawat
4413 gold badges7 silver badges18 bronze badges
4 Answers
Reset to default 2Surely it will throw error, because you did not assign ref
to any DOM element, so ref.current
is always null
. Make sure to assign it to a DOM element like this:
const WorkPage = () => {
const ref = useRef(null);
useEffect(() => {
let element = ref.current;
const rotate = () => {
element.style.transform = `translateX(${-window.pageYOffset}px)`;
};
window.addEventListener("scroll", rotate);
return () => window.removeEventListener("scroll", rotate);
}, []);
// assign ref to one of your DOM element
return <div ref={ref}>your element</div>;
};
refs
is not guaranteed to be set on first render caveats-with-callback-refs so you need to check if the element exists before you try to manipulate it directly in the DOM
if (ref && ref.current) {
// do something
}
On first render ref is undefined. You can use optional chaining operator
let element = ref?.current;
You can try 2 things :
Firstly,
let element = ref.current;
if(element) {
const rotate = () => {
element.style.transform = `translateX(${-window.pageYOffset}px)`
};
}
........
Secondly,
If first doesn't work then debug to find out if your code is going inside the if statement added previously. If it's not going there then : In the initial rendering your element to which you might have passed the ref(I am assuming you have done that already) might not be available. So your ref.current and hence element variable will be undefined inside use effect. So to bypass that you execute your logic inside setTimeout() like below :
useEffect(() => {
setTimeout(() => {
let element = ref.current;
if(element){
const rotate = () => {
element.style.transform = `translateX(${-window.pageYOffset}px)`
};
}
window.addEventListener('scroll', rotate)
return () => window.removeEventListener('scroll', rotate)
}, [])
}, 200)
.......
本文标签: javascriptTypeError Cannot set properties of undefined (setting 39transform39)Stack Overflow
版权声明:本文标题:javascript - TypeError: Cannot set properties of undefined (setting 'transform') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744965193a2634902.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论