ref 第一次(mount)初始化,被赋值给 currentHook.memoizedState;随后 update 阶段,会把 memoizedState 传给新的 hook。
function updateWorkInProgressHook(): Hook {
...
const newHook: Hook = {
memoizedState: currentHook.memoizedState, // 传递memoizedState
baseState: currentHook.baseState,
queue: currentHook.queue,
baseUpdate: currentHook.baseUpdate,
next: null
}
...
}
function mountRef<T>(initialValue: T): { current: T } {
const hook = mountWorkInProgressHook()
const ref = { current: initialValue } // 对象
hook.memoizedState = ref
return ref
}
ref 的类型是{current: any},对象类型是引用传递,内部的属性 current 不会被拷贝。所以传递的过程中 current 的值不变。
import { useRef, useEffect } from "react"
const usePrevious = value => {
const ref = useRef()
useEffect(() => {
ref.current = value
}, [value])
return ref.current
}