Skip to content

Commit

Permalink
Dedupe legacy context warnings
Browse files Browse the repository at this point in the history
Similar to other warnings about legacy APIs, only raise a warning once per component.

.
  • Loading branch information
kassens committed Jul 9, 2024
1 parent 8aafbcf commit 553e031
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ function initModules() {
};
}

const {resetModules, itRenders, clientRenderOnBadMarkup} =
ReactDOMServerIntegrationUtils(initModules);
const {resetModules, itRenders} = ReactDOMServerIntegrationUtils(initModules);

function formatValue(val) {
if (val === null) {
Expand Down Expand Up @@ -105,7 +104,7 @@ describe('ReactDOMServerIntegrationLegacyContextDisabled', () => {
<RegularFn />
</span>
</LegacyProvider>,
render === clientRenderOnBadMarkup ? 4 : 3,
3,
);
expect(e.textContent).toBe('{}undefinedundefined');
expect(lifecycleContextLog).toEqual([]);
Expand Down
10 changes: 8 additions & 2 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ let didWarnAboutLegacyLifecyclesAndDerivedState;
let didWarnAboutUndefinedDerivedState;
let didWarnAboutDirectlyAssigningPropsToState;
let didWarnAboutContextTypeAndContextTypes;
let didWarnAboutContextTypes;
let didWarnAboutChildContextTypes;
let didWarnAboutInvalidateContextType;
let didWarnOnInvalidCallback;

Expand All @@ -93,6 +95,8 @@ if (__DEV__) {
didWarnAboutDirectlyAssigningPropsToState = new Set<string>();
didWarnAboutUndefinedDerivedState = new Set<string>();
didWarnAboutContextTypeAndContextTypes = new Set<string>();
didWarnAboutContextTypes = new Set<mixed>();
didWarnAboutChildContextTypes = new Set<mixed>();
didWarnAboutInvalidateContextType = new Set<string>();
didWarnOnInvalidCallback = new Set<string>();

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

if (disableLegacyContext) {
if (ctor.childContextTypes) {
if (ctor.childContextTypes && !didWarnAboutChildContextTypes.has(ctor)) {
didWarnAboutChildContextTypes.add(ctor);
console.error(
'%s uses the legacy childContextTypes API which was removed in React 19. ' +
'Use React.createContext() instead.',
name,
);
}
if (ctor.contextTypes) {
if (ctor.contextTypes && !didWarnAboutContextTypes.has(ctor)) {
didWarnAboutContextTypes.add(ctor);
console.error(
'%s uses the legacy contextTypes API which was removed in React 19. ' +
'Use React.createContext() with static contextType instead.',
Expand Down
10 changes: 8 additions & 2 deletions packages/react-server/src/ReactFizzClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ let didWarnAboutLegacyLifecyclesAndDerivedState;
let didWarnAboutUndefinedDerivedState;
let didWarnAboutDirectlyAssigningPropsToState;
let didWarnAboutContextTypeAndContextTypes;
let didWarnAboutContextTypes;
let didWarnAboutChildContextTypes;
let didWarnAboutInvalidateContextType;
let didWarnOnInvalidCallback;

Expand All @@ -36,6 +38,8 @@ if (__DEV__) {
didWarnAboutDirectlyAssigningPropsToState = new Set<string>();
didWarnAboutUndefinedDerivedState = new Set<string>();
didWarnAboutContextTypeAndContextTypes = new Set<mixed>();
didWarnAboutContextTypes = new Set<mixed>();
didWarnAboutChildContextTypes = new Set<mixed>();
didWarnAboutInvalidateContextType = new Set<mixed>();
didWarnOnInvalidCallback = new Set<string>();
}
Expand Down Expand Up @@ -362,14 +366,16 @@ function checkClassInstance(instance: any, ctor: any, newProps: any) {
}

if (disableLegacyContext) {
if (ctor.childContextTypes) {
if (ctor.childContextTypes && !didWarnAboutChildContextTypes.has(ctor)) {
didWarnAboutChildContextTypes.add(ctor);
console.error(
'%s uses the legacy childContextTypes API which was removed in React 19. ' +
'Use React.createContext() instead.',
name,
);
}
if (ctor.contextTypes) {
if (ctor.contextTypes && !didWarnAboutContextTypes.has(ctor)) {
didWarnAboutContextTypes.add(ctor);
console.error(
'%s uses the legacy contextTypes API which was removed in React 19. ' +
'Use React.createContext() with static contextType instead.',
Expand Down

0 comments on commit 553e031

Please sign in to comment.