Skip to content

Commit 415ee0e

Browse files
committedApr 18, 2024··
Backport legacy context deprecation warning
This backports a deprecation warning for legacy context, even when Strict Mode is not enabled. I didn't bother to update all the tests because the tests are in such a different state than what's on `main`, and on `main` we already updated the tests accordingly. So instead I silenced the warnings in our test config, like we've done for other warnings in the past.
1 parent 5894232 commit 415ee0e

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
 

‎packages/react-reconciler/src/ReactFiberClassComponent.new.js

+35
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ let warnOnInvalidCallback;
9696
let didWarnAboutDirectlyAssigningPropsToState;
9797
let didWarnAboutContextTypeAndContextTypes;
9898
let didWarnAboutInvalidateContextType;
99+
let didWarnAboutLegacyContext;
99100

100101
if (__DEV__) {
101102
didWarnAboutStateAssignmentForComponent = new Set();
@@ -106,6 +107,7 @@ if (__DEV__) {
106107
didWarnAboutUndefinedDerivedState = new Set();
107108
didWarnAboutContextTypeAndContextTypes = new Set();
108109
didWarnAboutInvalidateContextType = new Set();
110+
didWarnAboutLegacyContext = new Set();
109111

110112
const didWarnOnInvalidCallback = new Set();
111113

@@ -435,6 +437,39 @@ function checkClassInstance(workInProgress: Fiber, ctor: any, newProps: any) {
435437
);
436438
}
437439
} else {
440+
if (
441+
ctor.childContextTypes &&
442+
!didWarnAboutLegacyContext.has(ctor) &&
443+
// Strict Mode has its own warning for legacy context, so we can skip
444+
// this one.
445+
(workInProgress.mode & StrictLegacyMode) === NoMode
446+
) {
447+
didWarnAboutLegacyContext.add(ctor);
448+
console.error(
449+
'%s uses the legacy childContextTypes API which is no longer ' +
450+
'supported and will be removed in the next major release. Use ' +
451+
'React.createContext() instead\n\n.' +
452+
'Learn more about this warning here: https://reactjs.org/link/legacy-context',
453+
name,
454+
);
455+
}
456+
if (
457+
ctor.contextTypes &&
458+
!didWarnAboutLegacyContext.has(ctor) &&
459+
// Strict Mode has its own warning for legacy context, so we can skip
460+
// this one.
461+
(workInProgress.mode & StrictLegacyMode) === NoMode
462+
) {
463+
didWarnAboutLegacyContext.add(ctor);
464+
console.error(
465+
'%s uses the legacy contextTypes API which is no longer supported ' +
466+
'and will be removed in the next major release. Use ' +
467+
'React.createContext() with static contextType instead.\n\n' +
468+
'Learn more about this warning here: https://reactjs.org/link/legacy-context',
469+
name,
470+
);
471+
}
472+
438473
if (instance.contextTypes) {
439474
console.error(
440475
'contextTypes was defined as an instance property on %s. Use a static ' +

‎packages/react-reconciler/src/ReactFiberClassComponent.old.js

+35
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ let warnOnInvalidCallback;
9696
let didWarnAboutDirectlyAssigningPropsToState;
9797
let didWarnAboutContextTypeAndContextTypes;
9898
let didWarnAboutInvalidateContextType;
99+
let didWarnAboutLegacyContext;
99100

100101
if (__DEV__) {
101102
didWarnAboutStateAssignmentForComponent = new Set();
@@ -106,6 +107,7 @@ if (__DEV__) {
106107
didWarnAboutUndefinedDerivedState = new Set();
107108
didWarnAboutContextTypeAndContextTypes = new Set();
108109
didWarnAboutInvalidateContextType = new Set();
110+
didWarnAboutLegacyContext = new Set();
109111

110112
const didWarnOnInvalidCallback = new Set();
111113

@@ -435,6 +437,39 @@ function checkClassInstance(workInProgress: Fiber, ctor: any, newProps: any) {
435437
);
436438
}
437439
} else {
440+
if (
441+
ctor.childContextTypes &&
442+
!didWarnAboutLegacyContext.has(ctor) &&
443+
// Strict Mode has its own warning for legacy context, so we can skip
444+
// this one.
445+
(workInProgress.mode & StrictLegacyMode) === NoMode
446+
) {
447+
didWarnAboutLegacyContext.add(ctor);
448+
console.error(
449+
'%s uses the legacy childContextTypes API which is no longer ' +
450+
'supported and will be removed in the next major release. Use ' +
451+
'React.createContext() instead\n\n.' +
452+
'Learn more about this warning here: https://reactjs.org/link/legacy-context',
453+
name,
454+
);
455+
}
456+
if (
457+
ctor.contextTypes &&
458+
!didWarnAboutLegacyContext.has(ctor) &&
459+
// Strict Mode has its own warning for legacy context, so we can skip
460+
// this one.
461+
(workInProgress.mode & StrictLegacyMode) === NoMode
462+
) {
463+
didWarnAboutLegacyContext.add(ctor);
464+
console.error(
465+
'%s uses the legacy contextTypes API which is no longer supported ' +
466+
'and will be removed in the next major release. Use ' +
467+
'React.createContext() with static contextType instead.\n\n' +
468+
'Learn more about this warning here: https://reactjs.org/link/legacy-context',
469+
name,
470+
);
471+
}
472+
438473
if (instance.contextTypes) {
439474
console.error(
440475
'contextTypes was defined as an instance property on %s. Use a static ' +

‎scripts/jest/shouldIgnoreConsoleError.js

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ module.exports = function shouldIgnoreConsoleError(format, args) {
2323
// We haven't finished migrating our tests to use createRoot.
2424
return true;
2525
}
26+
if (
27+
format.indexOf(
28+
'uses the legacy contextTypes API which is no longer supported and will be removed'
29+
) !== -1 ||
30+
format.indexOf(
31+
'uses the legacy childContextTypes API which is no longer supported and will be removed'
32+
) !== -1
33+
) {
34+
// This is a backported warning. In `main`, there's a different warning
35+
// (and it's fully tested). Not going to bother upgrading all the tests
36+
// on this old release branch, so let's just silence it instead.
37+
return true;
38+
}
2639
}
2740
} else {
2841
if (

0 commit comments

Comments
 (0)
Please sign in to comment.