1. export function createContext<T>(
    2. // 默认值
    3. defaultValue: T,
    4. // 判断新老变化
    5. calculateChangedBits: ?(a: T, b: T) => number,
    6. ): ReactContext<T> {
    7. // calculateChangedBits 类型校验
    8. // ...
    9. // 创建 context 实例,同时其实也是 Consumer
    10. const context: ReactContext<T> = {
    11. $$typeof: REACT_CONTEXT_TYPE,
    12. _calculateChangedBits: calculateChangedBits,
    13. // As a workaround to support multiple concurrent renderers, we categorize
    14. // some renderers as primary and others as secondary. We only expect
    15. // there to be two concurrent renderers at most: React Native (primary) and
    16. // Fabric (secondary); React DOM (primary) and React ART (secondary).
    17. // Secondary renderers store their context values on separate fields.
    18. _currentValue: defaultValue,
    19. _currentValue2: defaultValue,
    20. // These are circular
    21. Provider: (null: any),
    22. Consumer: (null: any),
    23. };
    24. // 创建 Provide 元素
    25. context.Provider = {
    26. $$typeof: REACT_PROVIDER_TYPE,
    27. _context: context,
    28. };
    29. // 开发时非法使用的错误抛出
    30. // ...
    31. // 将 Consumer 指向 context 自身
    32. context.Consumer = context;
    33. return context;
    34. }