Skip to content

Commit 4233a4c

Browse files
committed
Disable legacy context
1 parent 493610f commit 4233a4c

8 files changed

Lines changed: 80 additions & 69 deletions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ describe('ReactComponentLifeCycle', () => {
984984
});
985985

986986
if (!require('shared/ReactFeatureFlags').disableModulePatternComponents) {
987+
// @gate !disableLegacyContext
987988
it('calls effects on module-pattern component', function () {
988989
const log = [];
989990

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

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -799,45 +799,48 @@ describe('ReactErrorBoundaries', () => {
799799
expect(container.firstChild.textContent).toBe('Caught an error: Hello.');
800800
});
801801

802-
// @gate !disableModulePatternComponents
803-
// @gate !disableLegacyContext
804-
it('renders an error state if module-style context provider throws in componentWillMount', () => {
805-
function BrokenComponentWillMountWithContext() {
806-
return {
807-
getChildContext() {
808-
return {foo: 42};
809-
},
810-
render() {
811-
return <div>{this.props.children}</div>;
812-
},
813-
UNSAFE_componentWillMount() {
814-
throw new Error('Hello');
815-
},
802+
if (
803+
!require('shared/ReactFeatureFlags').disableModulePatternComponents &&
804+
!require('shared/ReactFeatureFlags').disableLegacyContext
805+
) {
806+
it('renders an error state if module-style context provider throws in componentWillMount', () => {
807+
function BrokenComponentWillMountWithContext() {
808+
return {
809+
getChildContext() {
810+
return {foo: 42};
811+
},
812+
render() {
813+
return <div>{this.props.children}</div>;
814+
},
815+
UNSAFE_componentWillMount() {
816+
throw new Error('Hello');
817+
},
818+
};
819+
}
820+
BrokenComponentWillMountWithContext.childContextTypes = {
821+
foo: PropTypes.number,
816822
};
817-
}
818-
BrokenComponentWillMountWithContext.childContextTypes = {
819-
foo: PropTypes.number,
820-
};
821823

822-
const container = document.createElement('div');
823-
expect(() =>
824-
ReactDOM.render(
825-
<ErrorBoundary>
826-
<BrokenComponentWillMountWithContext />
827-
</ErrorBoundary>,
828-
container,
829-
),
830-
).toErrorDev(
831-
'Warning: The <BrokenComponentWillMountWithContext /> component appears to be a function component that ' +
832-
'returns a class instance. ' +
833-
'Change BrokenComponentWillMountWithContext to a class that extends React.Component instead. ' +
834-
"If you can't use a class try assigning the prototype on the function as a workaround. " +
835-
'`BrokenComponentWillMountWithContext.prototype = React.Component.prototype`. ' +
836-
"Don't use an arrow function since it cannot be called with `new` by React.",
837-
);
824+
const container = document.createElement('div');
825+
expect(() =>
826+
ReactDOM.render(
827+
<ErrorBoundary>
828+
<BrokenComponentWillMountWithContext />
829+
</ErrorBoundary>,
830+
container,
831+
),
832+
).toErrorDev(
833+
'Warning: The <BrokenComponentWillMountWithContext /> component appears to be a function component that ' +
834+
'returns a class instance. ' +
835+
'Change BrokenComponentWillMountWithContext to a class that extends React.Component instead. ' +
836+
"If you can't use a class try assigning the prototype on the function as a workaround. " +
837+
'`BrokenComponentWillMountWithContext.prototype = React.Component.prototype`. ' +
838+
"Don't use an arrow function since it cannot be called with `new` by React.",
839+
);
838840

839-
expect(container.firstChild.textContent).toBe('Caught an error: Hello.');
840-
});
841+
expect(container.firstChild.textContent).toBe('Caught an error: Hello.');
842+
});
843+
}
841844

842845
it('mounts the error message if mounting fails', () => {
843846
function renderError(error) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,10 @@ describe('ReactLegacyErrorBoundaries', () => {
825825
expect(container.firstChild.textContent).toBe('Caught an error: Hello.');
826826
});
827827

828-
if (!require('shared/ReactFeatureFlags').disableModulePatternComponents) {
828+
if (
829+
!require('shared/ReactFeatureFlags').disableModulePatternComponents &&
830+
!require('shared/ReactFeatureFlags').disableLegacyContext
831+
) {
829832
it('renders an error state if module-style context provider throws in componentWillMount', () => {
830833
function BrokenComponentWillMountWithContext() {
831834
return {

packages/react-reconciler/src/__tests__/ReactIncrementalErrorHandling-test.internal.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,36 +1756,40 @@ describe('ReactIncrementalErrorHandling', () => {
17561756
);
17571757
});
17581758

1759-
// @gate !disableModulePatternComponents
1760-
it('handles error thrown inside getDerivedStateFromProps of a module-style context provider', async () => {
1761-
function Provider() {
1762-
return {
1763-
getChildContext() {
1764-
return {foo: 'bar'};
1765-
},
1766-
render() {
1767-
return 'Hi';
1768-
},
1759+
if (
1760+
!require('shared/ReactFeatureFlags').disableModulePatternComponents &&
1761+
!require('shared/ReactFeatureFlags').disableLegacyContext
1762+
) {
1763+
it('handles error thrown inside getDerivedStateFromProps of a module-style context provider', async () => {
1764+
function Provider() {
1765+
return {
1766+
getChildContext() {
1767+
return {foo: 'bar'};
1768+
},
1769+
render() {
1770+
return 'Hi';
1771+
},
1772+
};
1773+
}
1774+
Provider.childContextTypes = {
1775+
x: () => {},
1776+
};
1777+
Provider.getDerivedStateFromProps = () => {
1778+
throw new Error('Oops!');
17691779
};
1770-
}
1771-
Provider.childContextTypes = {
1772-
x: () => {},
1773-
};
1774-
Provider.getDerivedStateFromProps = () => {
1775-
throw new Error('Oops!');
1776-
};
17771780

1778-
ReactNoop.render(<Provider />);
1779-
await expect(async () => {
1780-
await waitForThrow('Oops!');
1781-
}).toErrorDev([
1782-
'Warning: The <Provider /> component appears to be a function component that returns a class instance. ' +
1783-
'Change Provider to a class that extends React.Component instead. ' +
1784-
"If you can't use a class try assigning the prototype on the function as a workaround. " +
1785-
'`Provider.prototype = React.Component.prototype`. ' +
1786-
"Don't use an arrow function since it cannot be called with `new` by React.",
1787-
]);
1788-
});
1781+
ReactNoop.render(<Provider />);
1782+
await expect(async () => {
1783+
await waitForThrow('Oops!');
1784+
}).toErrorDev([
1785+
'Warning: The <Provider /> component appears to be a function component that returns a class instance. ' +
1786+
'Change Provider to a class that extends React.Component instead. ' +
1787+
"If you can't use a class try assigning the prototype on the function as a workaround. " +
1788+
'`Provider.prototype = React.Component.prototype`. ' +
1789+
"Don't use an arrow function since it cannot be called with `new` by React.",
1790+
]);
1791+
});
1792+
}
17891793

17901794
it('uncaught errors should be discarded if the render is aborted', async () => {
17911795
const root = ReactNoop.createRoot();

packages/shared/ReactFeatureFlags.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const createRootStrictEffectsByDefault = false;
150150

151151
export const disableModulePatternComponents = false;
152152

153-
export const disableLegacyContext = false;
153+
export const disableLegacyContext = true;
154154

155155
export const enableUseRefAccessWarning = false;
156156

packages/shared/forks/ReactFeatureFlags.native-oss.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const enableSchedulerDebugging = false;
3535
export const enableScopeAPI = false;
3636
export const enableCreateEventHandleAPI = false;
3737
export const enableSuspenseCallback = false;
38-
export const disableLegacyContext = false;
38+
export const disableLegacyContext = true;
3939
export const enableTrustedTypesIntegration = false;
4040
export const disableTextareaChildren = false;
4141
export const disableModulePatternComponents = false;

packages/shared/forks/ReactFeatureFlags.test-renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const enableSchedulerDebugging = false;
3535
export const enableScopeAPI = false;
3636
export const enableCreateEventHandleAPI = false;
3737
export const enableSuspenseCallback = false;
38-
export const disableLegacyContext = false;
38+
export const disableLegacyContext = true;
3939
export const enableTrustedTypesIntegration = false;
4040
export const disableTextareaChildren = false;
4141
export const disableModulePatternComponents = false;

packages/shared/forks/ReactFeatureFlags.test-renderer.native.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const enableSchedulerDebugging = false;
3535
export const enableScopeAPI = false;
3636
export const enableCreateEventHandleAPI = false;
3737
export const enableSuspenseCallback = false;
38-
export const disableLegacyContext = false;
38+
export const disableLegacyContext = true;
3939
export const enableTrustedTypesIntegration = false;
4040
export const disableTextareaChildren = false;
4141
export const disableModulePatternComponents = false;

0 commit comments

Comments
 (0)