Skip to content

Commit

Permalink
Backport legacy context deprecation warning
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
acdlite committed Apr 18, 2024
1 parent 5894232 commit 415ee0e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/react-reconciler/src/ReactFiberClassComponent.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ let warnOnInvalidCallback;
let didWarnAboutDirectlyAssigningPropsToState;
let didWarnAboutContextTypeAndContextTypes;
let didWarnAboutInvalidateContextType;
let didWarnAboutLegacyContext;

if (__DEV__) {
didWarnAboutStateAssignmentForComponent = new Set();
Expand All @@ -106,6 +107,7 @@ if (__DEV__) {
didWarnAboutUndefinedDerivedState = new Set();
didWarnAboutContextTypeAndContextTypes = new Set();
didWarnAboutInvalidateContextType = new Set();
didWarnAboutLegacyContext = new Set();

const didWarnOnInvalidCallback = new Set();

Expand Down Expand Up @@ -435,6 +437,39 @@ function checkClassInstance(workInProgress: Fiber, ctor: any, newProps: any) {
);
}
} else {
if (
ctor.childContextTypes &&
!didWarnAboutLegacyContext.has(ctor) &&
// Strict Mode has its own warning for legacy context, so we can skip
// this one.
(workInProgress.mode & StrictLegacyMode) === NoMode
) {
didWarnAboutLegacyContext.add(ctor);
console.error(
'%s uses the legacy childContextTypes API which is no longer ' +
'supported and will be removed in the next major release. Use ' +
'React.createContext() instead\n\n.' +
'Learn more about this warning here: https://reactjs.org/link/legacy-context',
name,
);
}
if (
ctor.contextTypes &&
!didWarnAboutLegacyContext.has(ctor) &&
// Strict Mode has its own warning for legacy context, so we can skip
// this one.
(workInProgress.mode & StrictLegacyMode) === NoMode
) {
didWarnAboutLegacyContext.add(ctor);
console.error(
'%s uses the legacy contextTypes API which is no longer supported ' +
'and will be removed in the next major release. Use ' +
'React.createContext() with static contextType instead.\n\n' +
'Learn more about this warning here: https://reactjs.org/link/legacy-context',
name,
);
}

if (instance.contextTypes) {
console.error(
'contextTypes was defined as an instance property on %s. Use a static ' +
Expand Down
35 changes: 35 additions & 0 deletions packages/react-reconciler/src/ReactFiberClassComponent.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ let warnOnInvalidCallback;
let didWarnAboutDirectlyAssigningPropsToState;
let didWarnAboutContextTypeAndContextTypes;
let didWarnAboutInvalidateContextType;
let didWarnAboutLegacyContext;

if (__DEV__) {
didWarnAboutStateAssignmentForComponent = new Set();
Expand All @@ -106,6 +107,7 @@ if (__DEV__) {
didWarnAboutUndefinedDerivedState = new Set();
didWarnAboutContextTypeAndContextTypes = new Set();
didWarnAboutInvalidateContextType = new Set();
didWarnAboutLegacyContext = new Set();

const didWarnOnInvalidCallback = new Set();

Expand Down Expand Up @@ -435,6 +437,39 @@ function checkClassInstance(workInProgress: Fiber, ctor: any, newProps: any) {
);
}
} else {
if (
ctor.childContextTypes &&
!didWarnAboutLegacyContext.has(ctor) &&
// Strict Mode has its own warning for legacy context, so we can skip
// this one.
(workInProgress.mode & StrictLegacyMode) === NoMode
) {
didWarnAboutLegacyContext.add(ctor);
console.error(
'%s uses the legacy childContextTypes API which is no longer ' +
'supported and will be removed in the next major release. Use ' +
'React.createContext() instead\n\n.' +
'Learn more about this warning here: https://reactjs.org/link/legacy-context',
name,
);
}
if (
ctor.contextTypes &&
!didWarnAboutLegacyContext.has(ctor) &&
// Strict Mode has its own warning for legacy context, so we can skip
// this one.
(workInProgress.mode & StrictLegacyMode) === NoMode
) {
didWarnAboutLegacyContext.add(ctor);
console.error(
'%s uses the legacy contextTypes API which is no longer supported ' +
'and will be removed in the next major release. Use ' +
'React.createContext() with static contextType instead.\n\n' +
'Learn more about this warning here: https://reactjs.org/link/legacy-context',
name,
);
}

if (instance.contextTypes) {
console.error(
'contextTypes was defined as an instance property on %s. Use a static ' +
Expand Down
13 changes: 13 additions & 0 deletions scripts/jest/shouldIgnoreConsoleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ module.exports = function shouldIgnoreConsoleError(format, args) {
// We haven't finished migrating our tests to use createRoot.
return true;
}
if (
format.indexOf(
'uses the legacy contextTypes API which is no longer supported and will be removed'
) !== -1 ||
format.indexOf(
'uses the legacy childContextTypes API which is no longer supported and will be removed'
) !== -1
) {
// This is a backported warning. In `main`, there's a different warning
// (and it's fully tested). Not going to bother upgrading all the tests
// on this old release branch, so let's just silence it instead.
return true;
}
}
} else {
if (
Expand Down

0 comments on commit 415ee0e

Please sign in to comment.