Skip to content

Commit d74094d

Browse files
committed
Warn about legacy context when legacy context is not disabled
For environments that still have legacy contexts available, this adds a warning to make the remaining call sites easier to locate and encourage upgrades. .
1 parent 553e031 commit d74094d

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

packages/react-dom/src/__tests__/ReactFunctionComponent-test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let PropTypes;
1313
let React;
1414
let ReactDOMClient;
1515
let act;
16+
let assertConsoleErrorDev;
1617

1718
function FunctionComponent(props) {
1819
return <div>{props.name}</div>;
@@ -24,7 +25,7 @@ describe('ReactFunctionComponent', () => {
2425
PropTypes = require('prop-types');
2526
React = require('react');
2627
ReactDOMClient = require('react-dom/client');
27-
act = require('internal-test-utils').act;
28+
({act, assertConsoleErrorDev} = require('internal-test-utils'));
2829
});
2930

3031
it('should render stateless component', async () => {
@@ -109,6 +110,10 @@ describe('ReactFunctionComponent', () => {
109110
root.render(<GrandParent test="test" />);
110111
});
111112

113+
assertConsoleErrorDev([
114+
'Child uses the legacy contextTypes API which will soon be removed. Use React.createContext() with static contextType instead.',
115+
]);
116+
112117
expect(el.textContent).toBe('test');
113118

114119
await act(() => {
@@ -472,6 +477,9 @@ describe('ReactFunctionComponent', () => {
472477
await act(() => {
473478
root.render(<Parent />);
474479
});
480+
assertConsoleErrorDev([
481+
'Child uses the legacy contextTypes API which will be removed soon. Use React.createContext() with React.useContext() instead.',
482+
]);
475483
expect(el.textContent).toBe('en');
476484
});
477485

packages/react-reconciler/src/ReactFiberBeginWork.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,12 +1130,20 @@ function updateFunctionComponent(
11301130
// in updateFuntionComponent but only on mount
11311131
validateFunctionComponentInDev(workInProgress, workInProgress.type);
11321132

1133-
if (disableLegacyContext && Component.contextTypes) {
1134-
console.error(
1135-
'%s uses the legacy contextTypes API which was removed in React 19. ' +
1136-
'Use React.createContext() with React.useContext() instead.',
1137-
getComponentNameFromType(Component) || 'Unknown',
1138-
);
1133+
if (Component.contextTypes) {
1134+
if (disableLegacyContext) {
1135+
console.error(
1136+
'%s uses the legacy contextTypes API which was removed in React 19. ' +
1137+
'Use React.createContext() with React.useContext() instead.',
1138+
getComponentNameFromType(Component) || 'Unknown',
1139+
);
1140+
} else {
1141+
console.error(
1142+
'%s uses the legacy contextTypes API which will be removed soon. ' +
1143+
'Use React.createContext() with React.useContext() instead.',
1144+
getComponentNameFromType(Component) || 'Unknown',
1145+
);
1146+
}
11391147
}
11401148
}
11411149
}

packages/react-reconciler/src/ReactFiberClassComponent.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,22 @@ function checkClassInstance(workInProgress: Fiber, ctor: any, newProps: any) {
426426
name,
427427
);
428428
}
429+
if (ctor.childContextTypes && !didWarnAboutChildContextTypes.has(ctor)) {
430+
didWarnAboutChildContextTypes.add(ctor);
431+
console.error(
432+
'%s uses the legacy childContextTypes API which will soon be removed. ' +
433+
'Use React.createContext() instead.',
434+
name,
435+
);
436+
}
437+
if (ctor.contextTypes && !didWarnAboutContextTypes.has(ctor)) {
438+
didWarnAboutContextTypes.add(ctor);
439+
console.error(
440+
'%s uses the legacy contextTypes API which will soon be removed. ' +
441+
'Use React.createContext() with static contextType instead.',
442+
name,
443+
);
444+
}
429445
}
430446

431447
if (typeof instance.componentShouldUpdate === 'function') {

packages/react-server/src/ReactFizzClassComponent.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,22 @@ function checkClassInstance(instance: any, ctor: any, newProps: any) {
403403
name,
404404
);
405405
}
406+
if (ctor.childContextTypes && !didWarnAboutChildContextTypes.has(ctor)) {
407+
didWarnAboutChildContextTypes.add(ctor);
408+
console.error(
409+
'%s uses the legacy childContextTypes API which will soon be removed. ' +
410+
'Use React.createContext() instead.',
411+
name,
412+
);
413+
}
414+
if (ctor.contextTypes && !didWarnAboutContextTypes.has(ctor)) {
415+
didWarnAboutContextTypes.add(ctor);
416+
console.error(
417+
'%s uses the legacy contextTypes API which will soon be removed. ' +
418+
'Use React.createContext() with static contextType instead.',
419+
name,
420+
);
421+
}
406422
}
407423

408424
if (typeof instance.componentShouldUpdate === 'function') {

packages/react/src/__tests__/ReactES6Class-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let PropTypes;
1313
let React;
1414
let ReactDOM;
1515
let ReactDOMClient;
16+
let assertConsoleErrorDev;
1617

1718
describe('ReactES6Class', () => {
1819
let container;
@@ -30,6 +31,7 @@ describe('ReactES6Class', () => {
3031
React = require('react');
3132
ReactDOM = require('react-dom');
3233
ReactDOMClient = require('react-dom/client');
34+
({assertConsoleErrorDev} = require('internal-test-utils'));
3335
container = document.createElement('div');
3436
root = ReactDOMClient.createRoot(container);
3537
attachedListener = null;
@@ -287,6 +289,11 @@ describe('ReactES6Class', () => {
287289
className: PropTypes.string,
288290
};
289291
runTest(<Outer />, 'SPAN', 'foo');
292+
293+
assertConsoleErrorDev([
294+
'Outer uses the legacy childContextTypes API which will soon be removed. Use React.createContext() instead.',
295+
'Foo uses the legacy contextTypes API which will soon be removed. Use React.createContext() with static contextType instead.',
296+
]);
290297
});
291298
}
292299

@@ -579,6 +586,10 @@ describe('ReactES6Class', () => {
579586
}
580587
Foo.childContextTypes = {bar: PropTypes.string};
581588
runTest(<Foo />, 'DIV', 'bar-through-context');
589+
assertConsoleErrorDev([
590+
'Foo uses the legacy childContextTypes API which will soon be removed. Use React.createContext() instead.',
591+
'Bar uses the legacy contextTypes API which will soon be removed. Use React.createContext() with static contextType instead.',
592+
]);
582593
});
583594
}
584595

0 commit comments

Comments
 (0)