Open wwwind213 opened 5 years ago
useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.
本质上 createRef 和 useRef(mountRef) 就是创建一个 {current: initialState} 对象然后返回
useRef 本身经历两个阶段:mountRef 和 updateRef,updateRef 就是把 memoziedState(上面创建的对象)返回。
在 reconciler 阶段,如果有 ref 绑定,会打一个 efftag:Ref
// 位操作标明有 ref 的变更标志位
function markRef(workInProgress: Fiber) {
workInProgress.effectTag |= Ref;
}
在 commit 阶段,会通过判断是否存在 Ref 的副作用进行对应的处理,class component 返回类实例,而 DOM 则返回 DOM
他返回一个 {current: val }
对象,一般是用来获取真实DOM结构的,也能存储一些始终不应该改变的值。比如说 setTimeout 返回的定时器。可以存储在这里面,在其他事件回调中清除。
[react] 请说说什么是useRef?