Skip to content

Commit 553e031

Browse files
committed
Dedupe legacy context warnings
Similar to other warnings about legacy APIs, only raise a warning once per component. .
1 parent 8aafbcf commit 553e031

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

packages/react-dom/src/__tests__/ReactDOMServerIntegrationLegacyContextDisabled-test.internal.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ function initModules() {
3434
};
3535
}
3636

37-
const {resetModules, itRenders, clientRenderOnBadMarkup} =
38-
ReactDOMServerIntegrationUtils(initModules);
37+
const {resetModules, itRenders} = ReactDOMServerIntegrationUtils(initModules);
3938

4039
function formatValue(val) {
4140
if (val === null) {
@@ -105,7 +104,7 @@ describe('ReactDOMServerIntegrationLegacyContextDisabled', () => {
105104
<RegularFn />
106105
</span>
107106
</LegacyProvider>,
108-
render === clientRenderOnBadMarkup ? 4 : 3,
107+
3,
109108
);
110109
expect(e.textContent).toBe('{}undefinedundefined');
111110
expect(lifecycleContextLog).toEqual([]);

packages/react-reconciler/src/ReactFiberClassComponent.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ let didWarnAboutLegacyLifecyclesAndDerivedState;
8282
let didWarnAboutUndefinedDerivedState;
8383
let didWarnAboutDirectlyAssigningPropsToState;
8484
let didWarnAboutContextTypeAndContextTypes;
85+
let didWarnAboutContextTypes;
86+
let didWarnAboutChildContextTypes;
8587
let didWarnAboutInvalidateContextType;
8688
let didWarnOnInvalidCallback;
8789

@@ -93,6 +95,8 @@ if (__DEV__) {
9395
didWarnAboutDirectlyAssigningPropsToState = new Set<string>();
9496
didWarnAboutUndefinedDerivedState = new Set<string>();
9597
didWarnAboutContextTypeAndContextTypes = new Set<string>();
98+
didWarnAboutContextTypes = new Set<mixed>();
99+
didWarnAboutChildContextTypes = new Set<mixed>();
96100
didWarnAboutInvalidateContextType = new Set<string>();
97101
didWarnOnInvalidCallback = new Set<string>();
98102

@@ -385,14 +389,16 @@ function checkClassInstance(workInProgress: Fiber, ctor: any, newProps: any) {
385389
}
386390

387391
if (disableLegacyContext) {
388-
if (ctor.childContextTypes) {
392+
if (ctor.childContextTypes && !didWarnAboutChildContextTypes.has(ctor)) {
393+
didWarnAboutChildContextTypes.add(ctor);
389394
console.error(
390395
'%s uses the legacy childContextTypes API which was removed in React 19. ' +
391396
'Use React.createContext() instead.',
392397
name,
393398
);
394399
}
395-
if (ctor.contextTypes) {
400+
if (ctor.contextTypes && !didWarnAboutContextTypes.has(ctor)) {
401+
didWarnAboutContextTypes.add(ctor);
396402
console.error(
397403
'%s uses the legacy contextTypes API which was removed in React 19. ' +
398404
'Use React.createContext() with static contextType instead.',

packages/react-server/src/ReactFizzClassComponent.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ let didWarnAboutLegacyLifecyclesAndDerivedState;
2626
let didWarnAboutUndefinedDerivedState;
2727
let didWarnAboutDirectlyAssigningPropsToState;
2828
let didWarnAboutContextTypeAndContextTypes;
29+
let didWarnAboutContextTypes;
30+
let didWarnAboutChildContextTypes;
2931
let didWarnAboutInvalidateContextType;
3032
let didWarnOnInvalidCallback;
3133

@@ -36,6 +38,8 @@ if (__DEV__) {
3638
didWarnAboutDirectlyAssigningPropsToState = new Set<string>();
3739
didWarnAboutUndefinedDerivedState = new Set<string>();
3840
didWarnAboutContextTypeAndContextTypes = new Set<mixed>();
41+
didWarnAboutContextTypes = new Set<mixed>();
42+
didWarnAboutChildContextTypes = new Set<mixed>();
3943
didWarnAboutInvalidateContextType = new Set<mixed>();
4044
didWarnOnInvalidCallback = new Set<string>();
4145
}
@@ -362,14 +366,16 @@ function checkClassInstance(instance: any, ctor: any, newProps: any) {
362366
}
363367

364368
if (disableLegacyContext) {
365-
if (ctor.childContextTypes) {
369+
if (ctor.childContextTypes && !didWarnAboutChildContextTypes.has(ctor)) {
370+
didWarnAboutChildContextTypes.add(ctor);
366371
console.error(
367372
'%s uses the legacy childContextTypes API which was removed in React 19. ' +
368373
'Use React.createContext() instead.',
369374
name,
370375
);
371376
}
372-
if (ctor.contextTypes) {
377+
if (ctor.contextTypes && !didWarnAboutContextTypes.has(ctor)) {
378+
didWarnAboutContextTypes.add(ctor);
373379
console.error(
374380
'%s uses the legacy contextTypes API which was removed in React 19. ' +
375381
'Use React.createContext() with static contextType instead.',

0 commit comments

Comments
 (0)