在使用效果中使用Ref Vs Ref. -Current有什么区别?
我有以下参考变量:
const renderRefs = useRef()
之间的差异是什么:
useEffect(() => {
}, [renderRefs.current])
两者
useEffect(() => {
}, [renderRefs])
I have the following ref variable :
const renderRefs = useRef()
What is the differenece beetween using:
useEffect(() => {
}, [renderRefs.current])
and
useEffect(() => {
}, [renderRefs])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您打算参考相同的参考 -
useref
返回的REF对象是稳定的。它不会在重新订阅者之间发生变化。由于始终是相同的,所以这等同于
.current
ref的属性中的任何内容都可能会在重新订阅中进行更改,具体取决于REF的使用方式。将.current
属性放在依赖项数组中的意思是:“在重新渲染后,如果值自先前渲染以来已更改,请运行效果回调。”例如,如果您有
单击按钮,则由于
setSomestate
而导致重新效率在依赖项数组中并运行效果回调。如果ref值曾经在最后一个渲染上为5,则在上面的点击处理程序内进行
refitem.current = 5
表示效果呼叫将在下一个下一个 not 运行渲染,因为前5个值等于当前值5。设置状态很重要。如果您不设置状态,则组件不会重新渲染,并且组件未重新渲染,则未检查依赖项数组值(也没有发生任何其他情况)。因此,仅执行
不足以触发效果回调。
Assuming you meant to refer to the same ref -
The ref object returned by
useRef
is stable. It will not change across re-renders. Since it's always the same, thisis equivalent to
Whatever exists in the
.current
property of the ref may well change across re-renders, depending on how the ref is used. Putting the.current
property in the dependency array means: "After a re-render, if the value has changed since the prior render, run the effect callback."For example, if you had
clicking the button would result in a re-render due to
setSomeState
, and then if the ref value used to be something other than 5 on the last render, React will detect a change in the dependency array and run the effect callback.If the ref value used to be 5 on the last render, then doing
refItem.current = 5
inside the click handler above means that the effect callback will not run after the next render, because the previous value of 5 is equal to the current value of 5.Setting state is important. If you don't set state, the component won't re-render, and if the component doesn't re-render, the dependency array values aren't checked (nor does anything else occur). So, doing only
is not sufficient to trigger the effect callback.