以客户端 useState
为例
- 声明
useState
,通过ReactCurrentDispatcher.current
来实现 ReactCurrentDispatcher.current
的赋值是在packages/react-reconciler/src/ReactFiberHooks
文件中区分挂载、更新和重新渲染分别实现的
挂载
1 | // packages/react-reconciler/src/ReactFiberHooks.new.js |
更新
1 | // file: packages/react-reconciler/src/ReactFiberHooks.new.js |
重渲染
dispatchAction
1 | function dispatchAction(fiber, queue, action) { |
basicStateReducer
1 | function basicStateReducer(state, action) { |
所有的 hooks
都是挂载在 ReactCurrentDispatcher
对象上的
1 | function Test() { |
编译后实际是把函数组件当作参数传递下去了