7
* @flow
8
*/
9
10
-import {REACT_PROVIDER_TYPE, REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
10
+import {
11
+ REACT_PROVIDER_TYPE,
12
+ REACT_CONSUMER_TYPE,
13
+ REACT_CONTEXT_TYPE,
14
+} from 'shared/ReactSymbols';
15
12
-import type {ReactProviderType} from 'shared/ReactTypes';
16
import type {ReactContext} from 'shared/ReactTypes';
17
+import {enableRenderableContext} from 'shared/ReactFeatureFlags';
18
19
export function createContext<T>(defaultValue: T): ReactContext<T> {
20
// TODO: Second argument used to be an optional `calculateChangedBits`
37
Consumer: (null: any),
38
};
39
36
- context.Provider = {
37
- $$typeof: REACT_PROVIDER_TYPE,
38
- _context: context,
39
- };
40
-
41
- let hasWarnedAboutUsingNestedContextConsumers = false;
42
- let hasWarnedAboutUsingConsumerProvider = false;
43
- let hasWarnedAboutDisplayNameOnConsumer = false;
44
-
45
- if (__DEV__) {
46
- // A separate object, but proxies back to the original context object for
47
- // backwards compatibility. It has a different $$typeof, so we can properly
48
- // warn for the incorrect usage of Context as a Consumer.
49
- const Consumer = {
50
- $$typeof: REACT_CONTEXT_TYPE,
40
+ if (enableRenderableContext) {
41
+ context.Provider = context;
42
+ context.Consumer = {
43
+ $$typeof: REACT_CONSUMER_TYPE,
44
_context: context,
45
};
53
- // $FlowFixMe[prop-missing]: Flow complains about not setting a value, which is intentional here
54
- Object.defineProperties(Consumer, {
55
- Provider: {
56
- get() {
57
- if (!hasWarnedAboutUsingConsumerProvider) {
58
- hasWarnedAboutUsingConsumerProvider = true;
59
- console.error(
60
- 'Rendering <Context.Consumer.Provider> is not supported and will be removed in ' +
61
- 'a future major release. Did you mean to render <Context.Provider> instead?',
62
- );
63
- }
64
- return context.Provider;
65
- },
66
- set(_Provider: ReactProviderType<T>) {
67
- context.Provider = _Provider;
68
- },
69
- },
70
- _currentValue: {
71
- get() {
72
- return context._currentValue;
73
- },
74
- set(_currentValue: T) {
75
- context._currentValue = _currentValue;
76
- },
77
- },
78
- _currentValue2: {
79
- get() {
80
- return context._currentValue2;
81
- },
82
- set(_currentValue2: T) {
83
- context._currentValue2 = _currentValue2;
46
+ } else {
47
+ (context: any).Provider = {
48
+ $$typeof: REACT_PROVIDER_TYPE,
49
+ _context: context,
50
+ };
51
+ if (__DEV__) {
52
+ const Consumer: any = {
53
+ $$typeof: REACT_CONTEXT_TYPE,
54
+ _context: context,
55
+ };
56
+ Object.defineProperties(Consumer, {
57
+ Provider: {
58
+ get() {
59
+ return context.Provider;
60
+ },
61
+ set(_Provider: any) {
62
+ context.Provider = _Provider;
63
+ },
64
},
85
- },
86
- _threadCount: {
87
- get() {
88
- return context._threadCount;
65
+ _currentValue: {
66
+ get() {
67
+ return context._currentValue;
68
+ },
69
+ set(_currentValue: T) {
70
+ context._currentValue = _currentValue;
71
+ },
72
},
90
- set(_threadCount: number) {
91
- context._threadCount = _threadCount;
73
+ _currentValue2: {
74
+ get() {
75
+ return context._currentValue2;
76
+ },
77
+ set(_currentValue2: T) {
78
+ context._currentValue2 = _currentValue2;
79
+ },
80
},
93
- },
94
- Consumer: {
95
- get() {
96
- if (!hasWarnedAboutUsingNestedContextConsumers) {
97
- hasWarnedAboutUsingNestedContextConsumers = true;
98
- console.error(
99
- 'Rendering <Context.Consumer.Consumer> is not supported and will be removed in ' +
100
- 'a future major release. Did you mean to render <Context.Consumer> instead?',
101
- );
102
- }
103
- return context.Consumer;
81
+ _threadCount: {
82
+ get() {
83
+ return context._threadCount;
84
+ },
85
+ set(_threadCount: number) {
86
+ context._threadCount = _threadCount;
87
+ },
88
},
105
- },
106
- displayName: {
107
- get() {
108
- return context.displayName;
89
+ Consumer: {
90
+ get() {
91
+ return context.Consumer;
92
+ },
93
},
110
- set(displayName: void | string) {
111
- if (!hasWarnedAboutDisplayNameOnConsumer) {
112
- console.warn(
113
- 'Setting `displayName` on Context.Consumer has no effect. ' +
114
- "You should set it directly on the context with Context.displayName = '%s'.",
115
- displayName,
116
- );
117
- hasWarnedAboutDisplayNameOnConsumer = true;
118
- }
94
+ displayName: {
95
+ get() {
96
+ return context.displayName;
97
+ },
98
+ set(displayName: void | string) {},
99
},
120
- },
121
- });
122
- // $FlowFixMe[prop-missing]: Flow complains about missing properties because it doesn't understand defineProperty
123
- context.Consumer = Consumer;
124
- } else {
125
- context.Consumer = context;
100
+ });
101
+ (context: any).Consumer = Consumer;
102
+ } else {
103
+ (context: any).Consumer = context;
104
+ }
105
}
106
107
if (__DEV__) {