Dedupe legacy context warnings (#30299)
Similar to other warnings about legacy APIs, only raise a warning once per component.
Jan Kassens committed
Jul 9, 2024 at 19:55 UTC
39e69dc665ef6f6dd1f9fe2f63348afb09694eab
3 files changed
+18
-7
packages/react-dom/src/__tests__/ReactDOMServerIntegrationLegacyContextDisabled-test.internal.js
+2
-3
@@ -34,8 +34,7 @@ function initModules() {
34
};
35
}
36
37
-const {resetModules, itRenders, clientRenderOnBadMarkup} =
38
- ReactDOMServerIntegrationUtils(initModules);
37
+const {resetModules, itRenders} = ReactDOMServerIntegrationUtils(initModules);
38
39
function formatValue(val) {
40
if (val === null) {
@@ -105,7 +104,7 @@ describe('ReactDOMServerIntegrationLegacyContextDisabled', () => {
104
<RegularFn />
105
</span>
106
</LegacyProvider>,
108
- render === clientRenderOnBadMarkup ? 4 : 3,
107
+ 3,
108
);
109
expect(e.textContent).toBe('{}undefinedundefined');
110
expect(lifecycleContextLog).toEqual([]);
packages/react-reconciler/src/ReactFiberClassComponent.js
+8
-2
@@ -82,6 +82,8 @@ let didWarnAboutLegacyLifecyclesAndDerivedState;
82
let didWarnAboutUndefinedDerivedState;
83
let didWarnAboutDirectlyAssigningPropsToState;
84
let didWarnAboutContextTypeAndContextTypes;
85
+let didWarnAboutContextTypes;
86
+let didWarnAboutChildContextTypes;
87
let didWarnAboutInvalidateContextType;
88
let didWarnOnInvalidCallback;
89
@@ -93,6 +95,8 @@ if (__DEV__) {
95
didWarnAboutDirectlyAssigningPropsToState = new Set<string>();
96
didWarnAboutUndefinedDerivedState = new Set<string>();
97
didWarnAboutContextTypeAndContextTypes = new Set<string>();
98
+ didWarnAboutContextTypes = new Set<mixed>();
99
+ didWarnAboutChildContextTypes = new Set<mixed>();
100
didWarnAboutInvalidateContextType = new Set<string>();
101
didWarnOnInvalidCallback = new Set<string>();
102
@@ -385,14 +389,16 @@ function checkClassInstance(workInProgress: Fiber, ctor: any, newProps: any) {
389
}
390
391
if (disableLegacyContext) {
388
- if (ctor.childContextTypes) {
392
+ if (ctor.childContextTypes && !didWarnAboutChildContextTypes.has(ctor)) {
393
+ didWarnAboutChildContextTypes.add(ctor);
394
console.error(
395
'%s uses the legacy childContextTypes API which was removed in React 19. ' +
396
'Use React.createContext() instead.',
397
name,
398
);
399
}
395
- if (ctor.contextTypes) {
400
+ if (ctor.contextTypes && !didWarnAboutContextTypes.has(ctor)) {
401
+ didWarnAboutContextTypes.add(ctor);
402
console.error(
403
'%s uses the legacy contextTypes API which was removed in React 19. ' +
404
'Use React.createContext() with static contextType instead.',
packages/react-server/src/ReactFizzClassComponent.js
+8
-2
@@ -26,6 +26,8 @@ let didWarnAboutLegacyLifecyclesAndDerivedState;
26
let didWarnAboutUndefinedDerivedState;
27
let didWarnAboutDirectlyAssigningPropsToState;
28
let didWarnAboutContextTypeAndContextTypes;
29
+let didWarnAboutContextTypes;
30
+let didWarnAboutChildContextTypes;
31
let didWarnAboutInvalidateContextType;
32
let didWarnOnInvalidCallback;
33
@@ -36,6 +38,8 @@ if (__DEV__) {
38
didWarnAboutDirectlyAssigningPropsToState = new Set<string>();
39
didWarnAboutUndefinedDerivedState = new Set<string>();
40
didWarnAboutContextTypeAndContextTypes = new Set<mixed>();
41
+ didWarnAboutContextTypes = new Set<mixed>();
42
+ didWarnAboutChildContextTypes = new Set<mixed>();
43
didWarnAboutInvalidateContextType = new Set<mixed>();
44
didWarnOnInvalidCallback = new Set<string>();
45
}
@@ -362,14 +366,16 @@ function checkClassInstance(instance: any, ctor: any, newProps: any) {
366
}
367
368
if (disableLegacyContext) {
365
- if (ctor.childContextTypes) {
369
+ if (ctor.childContextTypes && !didWarnAboutChildContextTypes.has(ctor)) {
370
+ didWarnAboutChildContextTypes.add(ctor);
371
console.error(
372
'%s uses the legacy childContextTypes API which was removed in React 19. ' +
373
'Use React.createContext() instead.',
374
name,
375
);
376
}
372
- if (ctor.contextTypes) {
377
+ if (ctor.contextTypes && !didWarnAboutContextTypes.has(ctor)) {
378
+ didWarnAboutContextTypes.add(ctor);
379
console.error(
380
'%s uses the legacy contextTypes API which was removed in React 19. ' +
381
'Use React.createContext() with static contextType instead.',