[英]useEffect does not go to if statements
Do not actually understand why this code part not working as it should.并不真正理解为什么这个代码部分不能正常工作。 This useEffect
block re-renders on every scrollY position.这个useEffect
块在每个 scrollY position 上重新渲染。 Also, I see that this code part: console.log(wrapperRef.current.style.opacity);
另外,我看到这个代码部分: console.log(wrapperRef.current.style.opacity);
should call if and else if statements, but it does not.应该调用 if 和 else if 语句,但它没有。
Here is the code:这是代码:
useEffect(() => { console.log(wrapperRef.current.style.opacity); if (wrapperRef.current.style.opacity === 0) { setIsHidden(true); console.log("true"); } else if (wrapperRef.current.style.opacity === 1) { setIsHidden(false); console.log("false"); } }, [position]);
As you can see here, style values are strings, but with the ===
operator you also check for type equality.正如您在此处看到的,样式值是字符串,但使用===
运算符您还可以检查类型是否相等。
This means you check that '0' === 0
which are not the same types and also why your check never enters the if body.这意味着您检查'0' === 0
这不是同一类型,以及为什么您的检查从不进入 if 正文。
Either check for '0'
or use the ==
operator which does ignore types.检查'0'
或使用忽略类型的==
运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.