invokeGuardedCallback
1 )概述
- 在 commit 阶段中在 DEV 环境中调用了
invokeGuardedCallback这个方法 - 这个方法是在开发过程中被使用,用于捕获错误,协助开发调试的方法
2 )概述
定位到 packages/shared/ReactErrorUtils.js#L41
进入 invokeGuardedCallback
const reporter = {
onError(error: mixed) {
hasError = true;
caughtError = error;
},
};
/**
* Call a function while guarding against errors that happens within it.
* Returns an error if it throws, otherwise null.
*
* In production, this is implemented using a try-catch. The reason we don't
* use a try-catch directly is so that we can swap out a different
* implementation in DEV mode.
*
* @param {String} name of the guard to use for logging or debugging
* @param {Function} func The function to invoke
* @param {*} context The context to use when calling the function
* @param {...*} args Arguments for function
*/
export function invokeGuardedCallback<A, B, C, D, E, F, Context>(
name: string | null,
func: (a: A, b: B, c: C, d: D, e: E, f: F) => mixed,
context: Context,
a: A,
b: B,
c: C,
d: D,
e: E,
f: F,
): void {
hasError = false;
caughtError = null;
invokeGuardedCallbackImpl.apply(reporter, arguments);
}
- 这个方法它实际调用了
invokeGuardedCallbackImpl.apply(reporter, arguments); - 这个
reporter,是定义的一个全局对象 - 内部初始化了两个变量
hasError和caughtError - 看下
invokeGuardedCallbackImpl// https://github.com/facebook/react/blob/v16.6.3/packages/shared/invokeGuardedCallbackImpl.js let invokeGuardedCallbackImpl = function<A, B, C, D, E, F, Context>( name: string | null, func: (a: A, b: B, c: C, d: D, e: E, f: F) => mixed, context: Context, a:

最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



