function resolveDispatcher() {
const dispatcher = ReactCurrentDispatcher.current;
if (__DEV__) {
if (dispatcher === null) {
console.error(
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
' one of the following reasons:\n' +
'1. You might have mismatching versions of React and the renderer (such as React DOM)\n' +
'2. You might be breaking the Rules of Hooks\n' +
'3. You might have more than one copy of React in the same app\n' +
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.',
);
}
}
// Will result in a null access error if accessed outside render phase. We
// intentionally don't throw our own error because this is in a hot path.
// Also helps ensure this is inlined.
return ((dispatcher: any): Dispatcher);
}
React 之 useState
首先,我们从什么地方了解 useState 的呢?
在开发中,我们会经常会在函数组件中看到上面的这段类似的代码,那他执行的原理是什么样的呢?
然后,看到它内部的话调用了resolveDispatcher方法,next step
进入这一步之后,看见调用了 ReactCurrentDispatcher.current, 然后,我们进入ReactCurrentDispatcher.js中😄