Skip to content

Commit

Permalink
Guard against legacy context not being supported in DevTools fixture (f…
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored and AndyPengc12 committed Apr 15, 2024
1 parent 31dca02 commit 7b1615e
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 7b1615e

Please sign in to comment.