认识
2023年06月10日
一、认识
useCallback
用于缓存函数,当组件重现渲染时不会重新创建该函数(防止父组件重新渲染导致函数引用发生变化)。用法如下所示:
const cachedFn = useCallback(fn, dependencies)
二、细节
2.1 hook
const hook = {
next: null,
baseQueue: null,
baseState: null,
updateQueue: null,
memoizedState: null
};
2.2 hook.memoizedState
hook.memoizedState
用于存储 [callback, depA]
数组
2.3 fiber.memoizedState
fiber.memoizedState
用于存储 hooks
链表