We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
如题
The text was updated successfully, but these errors were encountered:
https://segmentfault.com/a/1190000042711084
const Lazy = React.lazy(() => import("./LazyComponent")) <Suspense fallback={"loading"}> <Lazy/> // lazy 包装的组件 </Suspense>
为异步加载的组件提供容器,并在未加载完成时展示 fallback 展示的组件
do { try { workLoopSync(); break; } catch (thrownValue) { handleError(root, thrownValue); } } while (true);
错误捕获代码,是由 workLoopSync 阶段的兜底函数 handleError 来处理的
handleError 中
throwException( root, erroredWork.return, erroredWork, thrownValue, workInProgressRootRenderLanes, ); completeUnitOfWork(erroredWork); --- // 首先判断是否是为 promise if ( value !== null && typeof value === 'object' && typeof value.then === 'function' ) { const wakeable: Wakeable = (value: any); resetSuspendedComponent(sourceFiber, rootRenderLanes); // 获取到 Suspens 父组件 const suspenseBoundary = getNearestSuspenseBoundaryToCapture(returnFiber); if (suspenseBoundary !== null) { suspenseBoundary.flags &= ~ForceClientRender; // 给 Suspens 父组件 打上一些标记,让 Suspens 父组件知道已经有异常抛出,需要渲染 fallback markSuspenseBoundaryShouldCapture( suspenseBoundary, returnFiber, sourceFiber, root, rootRenderLanes, ); // We only attach ping listeners in concurrent mode. Legacy Suspense always // commits fallbacks synchronously, so there are no pings. if (suspenseBoundary.mode & ConcurrentMode) { attachPingListener(root, wakeable, rootRenderLanes); } // 将抛出的 promise 放入Suspense 父组件的 updateQueue 中,后续会遍历这个 queue 进行回调绑定 attachRetryListener(suspenseBoundary, root, wakeable, rootRenderLanes); return; } }
捕获错误后会给 Suspense 的父组件打上标签,将 promise 放入更新队列
Sorry, something went wrong.
No branches or pull requests
如题
The text was updated successfully, but these errors were encountered: