React 上下文
export function createContext<T>(defaultValue: T): ReactContext<T> {
// TODO: Second argument used to be an optional `calculateChangedBits`
// function. Warn to reserve for future use?
// TODO: 第二个参数以前是可选的 `calculateChangedBits` 函数。是否发出警告以保留未来使用?
const context: ReactContext<T> = {
$$typeof: REACT_CONTEXT_TYPE,
// As a workaround to support multiple concurrent renderers, we categorize
// some renderers as primary and others as secondary. We only expect
// there to be two concurrent renderers at most: React Native (primary) and
// Fabric (secondary); React DOM (primary) and React ART (secondary).
// Secondary renderers store their context values on separate fields.
//
// 为了支持多个并发渲染器,作为一种变通方法,我们将一些渲染器分类为主要渲染器,另一些为次要渲染器。
// 我们只期望最多存在两个并发渲染器:React Native(主要渲染器)和 Fabric(次要渲染器);React
// DOM(主要渲染器)和 React ART(次要渲染器)。次要渲染器将它们的上下文值存储在单独的字段中。
_currentValue: defaultValue,
_currentValue2: defaultValue,
// Used to track how many concurrent renderers this context currently
// supports within in a single renderer. Such as parallel server rendering.
// 用于跟踪此上下文当前在单个渲染器中支持的并发渲染器数量。例如并行服务器渲染。
_threadCount: 0,
// These are circular
// 这些是循环的
Provider: null as any,
Consumer: null as any,
};
context.Provider = context;
context.Consumer = {
$$typeof: REACT_CONSUMER_TYPE,
_context: context,
};
if (__DEV__) {
context._currentRenderer = null;
context._currentRenderer2 = null;
}
return context;
}