mirror of
https://github.com/facebook/react.git
synced 2026-02-26 18:58:05 +00:00
* Switch to JSX API for context 80% sure this will be the final API. Merging this now so we can get this into the next www sync in preparation for 16.3. * Promote context to a stable API
57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import {REACT_PROVIDER_TYPE, REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
|
|
|
|
import type {ReactContext} from 'shared/ReactTypes';
|
|
|
|
import warning from 'fbjs/lib/warning';
|
|
|
|
export function createContext<T>(
|
|
defaultValue: T,
|
|
calculateChangedBits: ?(a: T, b: T) => number,
|
|
): ReactContext<T> {
|
|
if (calculateChangedBits === undefined) {
|
|
calculateChangedBits = null;
|
|
} else {
|
|
if (__DEV__) {
|
|
warning(
|
|
calculateChangedBits === null ||
|
|
typeof calculateChangedBits === 'function',
|
|
'createContext: Expected the optional second argument to be a ' +
|
|
'function. Instead received: %s',
|
|
calculateChangedBits,
|
|
);
|
|
}
|
|
}
|
|
|
|
const context: ReactContext<T> = {
|
|
$$typeof: REACT_CONTEXT_TYPE,
|
|
calculateChangedBits,
|
|
defaultValue,
|
|
currentValue: defaultValue,
|
|
changedBits: 0,
|
|
// These are circular
|
|
Provider: (null: any),
|
|
Consumer: (null: any),
|
|
};
|
|
|
|
context.Provider = {
|
|
$$typeof: REACT_PROVIDER_TYPE,
|
|
context,
|
|
};
|
|
context.Consumer = context;
|
|
|
|
if (__DEV__) {
|
|
context._currentRenderer = null;
|
|
}
|
|
|
|
return context;
|
|
}
|