Skip to content

Commit f7831fe

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 f7831fe

File tree

1 file changed

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

1 file changed

+32
-4
lines changed

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

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

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

0 commit comments

Comments
 (0)