Open dailynodejs opened 3 years ago
开发包
HooksDispatcherOnMountInDEV = {
useState: function (initialState) {
// ...
}
}
prod 包
const HooksDispatcherOnMount: Dispatcher = {
useState: mountState,
}
调用了 mountWorkInProgressHook
function mountWorkInProgressHook() {
var hook = {
memoizedState: null,
baseState: null,
baseQueue: null,
queue: null,
next: null
};
if (workInProgressHook === null) {
// This is the first hook in the list
currentlyRenderingFiber$1.memoizedState = workInProgressHook = hook;
} else {
// Append to the end of the list
workInProgressHook = workInProgressHook.next = hook;
}
return workInProgressHook;
}
流程图
两个分支:
源码地址:https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.development.js
beginWork
IndeterminateComponent
这里对应的是
FunctionComponent
,所以才会和 hooks 关联updateFunctionComponent
renderWithHooks
这里会判断走
HooksDispatcherOnMountInDEV
还是HooksDispatcherOnUpdateInDEV
prod 包
https://github.com/facebook/react/blob/487f4bf2ee7c86176637544c5473328f96ca0ba2/packages/react-reconciler/src/ReactFiberHooks.js#L355
useState
resolveDispatcher
这里就用到了
ReactCurrentDispatcher.current
源码地址:https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.development.js
dev 环境
prod 环境
https://github.com/facebook/react/blob/487f4bf2ee7c86176637544c5473328f96ca0ba2/packages/react-reconciler/src/ReactFiberHooks.js#L1203
updateState
更新走的,依赖了
updateReducer
updateReducer
updateWorkInProgressHook