跳到主要内容

React Fiber 并发

一、作用

二、是否为旧版活动环境

备注
  • warnsIfNotActing 由宿主环境提供
export function isLegacyActEnvironment(fiber: Fiber): boolean {
if (__DEV__) {
// Legacy mode. We preserve the behavior of React 17's act. It assumes an
// act environment whenever `jest` is defined, but you can still turn off
// spurious warnings by setting IS_REACT_ACT_ENVIRONMENT explicitly
// to false.
//
// 兼容模式。我们保留 React 17 中 act 的行为。它假设每当定义了 `jest` 时都是一个 act 环境,
// 但你仍然可以通过显式将 IS_REACT_ACT_ENVIRONMENT 设置为 false 来关闭伪警告。

const isReactActEnvironmentGlobal =
typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined'
? IS_REACT_ACT_ENVIRONMENT
: undefined;

const jestIsDefined = typeof jest !== 'undefined';
return (
warnsIfNotActing && jestIsDefined && isReactActEnvironmentGlobal !== false
);
}
return false;
}

三、是否并发活动环境

备注
  • ReactSharedInternalsshared 提供
export function isConcurrentActEnvironment(): void | boolean {
if (__DEV__) {
const isReactActEnvironmentGlobal =
typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined'
? IS_REACT_ACT_ENVIRONMENT
: undefined;

if (
!isReactActEnvironmentGlobal &&
ReactSharedInternals.actQueue !== null
) {
// TODO: Include link to relevant documentation page.
// 待办:包含相关文档页面的链接。
console.error(
'The current testing environment is not configured to support ' +
'act(...)',
);
}
return isReactActEnvironmentGlobal;
}
return false;
}