Skip to content

Commit

Permalink
Guard against legacy context being not supported in DevTools fixture
Browse files Browse the repository at this point in the history
Legacy context was removed from experimental builds so the default setup would hide important fixtures.

Now it just assumes that any crash within the tree rendering legacy context is due to legacy context not being supported.

I didn't remove since this fixture can be used with older versions of React where we would want to check if legacy
context still works.
  • Loading branch information
eps1lon committed Mar 20, 2024
1 parent 5cec48e commit 061be1c
Showing 1 changed file with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,46 @@ class ModernClassContextConsumerWithUpdates extends Component<any> {
}
}

type LegacyContextState = {
supportsLegacyContext: boolean,
};
class LegacyContext extends React.Component {
state: LegacyContextState = {supportsLegacyContext: true};

static getDerivedStateFromError(error: any): LegacyContextState {
return {supportsLegacyContext: false};
}

componentDidCatch(error: any, info: any) {
console.info(
'Assuming legacy context is not supported in this React version due to: ',
error,
info,
);
}

render(): React.Node {
if (!this.state.supportsLegacyContext) {
return <p>This version of React does not support legacy context.</p>;
}

return (
<React.Fragment>
<LegacyContextProvider>
<LegacyContextConsumer />
</LegacyContextProvider>
<LegacyContextProviderWithUpdates />
</React.Fragment>
);
}
}

export default function Contexts(): React.Node {
return (
<div>
<h1>Contexts</h1>
<ul>
<LegacyContextProvider>
<LegacyContextConsumer />
</LegacyContextProvider>
<LegacyContextProviderWithUpdates />
<LegacyContext />
<ModernContext.Provider value={contextData}>
<ModernContext.Consumer>
{(value: $FlowFixMe) =>
Expand Down

0 comments on commit 061be1c

Please sign in to comment.