如题,比如 ReactFilberScheduler.js 中有一个全局变量 isRendering 变量在 requestWork 和 performWorkOnRoot 方法中有用到,requestWork 函数开头判断当 isRendering = true 的直接返回return;而在 performWorkOnRoot 的开头将 isRendering 置为 true 而在末尾置为 false,那么问题来了 requestWork 的开头对 isRendering 的判断有什么意义?function requestWork(root: FiberRoot, expirationTime: ExpirationTime) { addRootToSchedule(root, expirationTime); if (isRendering) { // Prevent reentrancy. Remaining work will be scheduled at the end of // the currently rendering batch. return; } //....Omitted code}function performWorkOnRoot(root: FiberRoot, expirationTime: ExpirationTime, isExpired: boolean) { invariant( !isRendering, 'performWorkOnRoot was called recursively. This error is likely caused ' + 'by a bug in React. Please file an issue.', ); isRendering = true; //...Omitted code isRendering = false;}
3 回答
pardon110
TA贡献1038条经验 获得超227个赞
很显然只是个标志位,控制代码执行流程,在一些异步执行需要同步代码的情况下用到。你需要知道,代码并不总是从上到下逐行执行,而异步执行在js中是常态
添加回答
举报
0/150
提交
取消