Skip to content

Commit 061be1c

Browse files
committed
Guard against legacy context being not supported in DevTools fixture
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.
1 parent 5cec48e commit 061be1c

File tree

1 file changed

+35
-4
lines changed
  • packages/react-devtools-shell/src/app/InspectableElements

1 file changed

+35
-4
lines changed

packages/react-devtools-shell/src/app/InspectableElements/Contexts.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,46 @@ class ModernClassContextConsumerWithUpdates extends Component<any> {
273273
}
274274
}
275275

276+
type LegacyContextState = {
277+
supportsLegacyContext: boolean,
278+
};
279+
class LegacyContext extends React.Component {
280+
state: LegacyContextState = {supportsLegacyContext: true};
281+
282+
static getDerivedStateFromError(error: any): LegacyContextState {
283+
return {supportsLegacyContext: false};
284+
}
285+
286+
componentDidCatch(error: any, info: any) {
287+
console.info(
288+
'Assuming legacy context is not supported in this React version due to: ',
289+
error,
290+
info,
291+
);
292+
}
293+
294+
render(): React.Node {
295+
if (!this.state.supportsLegacyContext) {
296+
return <p>This version of React does not support legacy context.</p>;
297+
}
298+
299+
return (
300+
<React.Fragment>
301+
<LegacyContextProvider>
302+
<LegacyContextConsumer />
303+
</LegacyContextProvider>
304+
<LegacyContextProviderWithUpdates />
305+
</React.Fragment>
306+
);
307+
}
308+
}
309+
276310
export default function Contexts(): React.Node {
277311
return (
278312
<div>
279313
<h1>Contexts</h1>
280314
<ul>
281-
<LegacyContextProvider>
282-
<LegacyContextConsumer />
283-
</LegacyContextProvider>
284-
<LegacyContextProviderWithUpdates />
315+
<LegacyContext />
285316
<ModernContext.Provider value={contextData}>
286317
<ModernContext.Consumer>
287318
{(value: $FlowFixMe) =>

0 commit comments

Comments
 (0)