File location: packages/react/SRC/ReactContext. Js

Making the address

Export function createContext<T>(// Default defaultValue: T, // calculateChangedBits method, using object.is () to calculate new and old context changes calculateChangedBits:? (a: T, b: T) => number, ): ReactContext<T> {// Null if not calculateChangedBits === undefined {calculateChangedBits = null; } else {if (__DEV__) {// Production environment If calculateChangedBits exist and it is not function, throw error if (calculateChangedBits! == null && typeof calculateChangedBits ! == 'function' ) { console.error( 'createContext: Expected the optional second argument to be a ' + 'function. Instead received: %s', calculateChangedBits, ); } } } const context: ReactContext<T> = { $$typeof: REACT_CONTEXT_TYPE, _calculateChangedBits: calculateChangedBits, // 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. /** * As a solution that supports multiple concurrent renderers, we categorize it as having some renderers as primary renderers and others as secondary renderers * Up to two concurrent renderers: React Native (primary) and secondary * React DOM (primary) and React ART (secondary) * Secondary renderers store their context values in separate fields */ _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: any), // Consumer: (null: any) Any), // Components that receive values}; context.Provider = { $$typeof: REACT_PROVIDER_TYPE, _context: context, }; let hasWarnedAboutUsingNestedContextConsumers = false; let hasWarnedAboutUsingConsumerProvider = false; let hasWarnedAboutDisplayNameOnConsumer = false; if (__DEV__) { // A separate object, but proxies back to the original context object for // backwards compatibility. It has a different $$typeof, so we can properly // warn for the incorrect usage of Context as a Consumer. const Consumer = { $$typeof: REACT_CONTEXT_TYPE, _context: context, _calculateChangedBits: context._calculateChangedBits, }; // $FlowFixMe: Flow complains about not setting a value, which is intentional here Object.defineProperties(Consumer, { Provider: { get() { if (! hasWarnedAboutUsingConsumerProvider) { hasWarnedAboutUsingConsumerProvider = true; console.error( 'Rendering <Context.Consumer.Provider> is not supported and will be removed in ' + 'a future major release. Did you mean to render <Context.Provider> instead? ', a); } return context.Provider; }, set(_Provider) { context.Provider = _Provider; }, }, _currentValue: { get() { return context._currentValue; }, set(_currentValue) { context._currentValue = _currentValue; }, }, _currentValue2: { get() { return context._currentValue2; }, set(_currentValue2) { context._currentValue2 = _currentValue2; }, }, _threadCount: { get() { return context._threadCount; }, set(_threadCount) { context._threadCount = _threadCount; }, }, Consumer: { get() { if (! hasWarnedAboutUsingNestedContextConsumers) { hasWarnedAboutUsingNestedContextConsumers = true; console.error( 'Rendering <Context.Consumer.Consumer> is not supported and will be removed in ' + 'a future major release. Did you mean to render <Context.Consumer> instead? ', a); } return context.Consumer; }, }, displayName: { get() { return context.displayName; }, set(displayName) { if (! hasWarnedAboutDisplayNameOnConsumer) { console.warn( 'Setting `displayName` on Context.Consumer has no effect. ' + "You should set it directly on the context with Context.displayName = '%s'.", displayName, ); hasWarnedAboutDisplayNameOnConsumer = true; ,}}}}); // $FlowFixMe: Flow complains about missing properties because it doesn't understand defineProperty context.Consumer = Consumer; } else { context.Consumer = context; } if (__DEV__) { context._currentRenderer = null; context._currentRenderer2 = null; } // return context; }Copy the code