mountCallback
typescript
function mountCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
// 创建hook
const hook = mountWorkInProgressHook();
const nextDeps = deps === undefined ? null : deps;
// 当包裹的函数以及依赖,存储在hook对象的memoizedState上
hook.memoizedState = [callback, nextDeps];
return callback;
}updateCallback
typescript
function updateCallback<T>(callback: T, deps: Array<mixed> | void | null): T {
const hook = updateWorkInProgressHook();
const nextDeps = deps === undefined ? null : deps;
const prevState = hook.memoizedState;
if (prevState !== null) {
if (nextDeps !== null) {
const prevDeps: Array<mixed> | null = prevState[1];
// 如果新老依赖没有改变,那么久返回上一次缓存的callback
if (areHookInputsEqual(nextDeps, prevDeps)) {
return prevState[0];
}
}
}
// 否则,返回新的callback
hook.memoizedState = [callback, nextDeps];
return callback;
}areHookInputsEqual
这里的is就是一个Object.is的实现
typescript
function areHookInputsEqual(
nextDeps: Array<mixed>,
prevDeps: Array<mixed> | null,
) {
if (prevDeps === null) {
return false;
}
for (let i = 0; i < prevDeps.length && i < nextDeps.length; i++) {
if (is(nextDeps[i], prevDeps[i])) {
continue;
}
return false;
}
return true;
}其实就是要遍历当前deps, 然后依次对比每一项是否相等