mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-24 11:10:01 +08:00
fd9bc6caa0
This comment was an accidental copy/pasta from https://reactjs.org/docs/error-boundaries.html. Also, it is inaccurate since the user (i.e. AntD user) cannot actually render any fallback component.
43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import * as React from 'react';
|
|
import Alert from '.';
|
|
|
|
interface ErrorBoundaryProps {
|
|
message?: React.ReactNode;
|
|
description?: React.ReactNode;
|
|
}
|
|
|
|
export default class ErrorBoundary extends React.Component<
|
|
ErrorBoundaryProps,
|
|
{
|
|
error?: Error | null;
|
|
info: {
|
|
componentStack?: string;
|
|
};
|
|
}
|
|
> {
|
|
state = {
|
|
error: undefined,
|
|
info: {
|
|
componentStack: '',
|
|
},
|
|
};
|
|
|
|
componentDidCatch(error: Error | null, info: object) {
|
|
this.setState({ error, info });
|
|
}
|
|
|
|
render() {
|
|
const { message, description, children } = this.props;
|
|
const { error, info } = this.state;
|
|
const componentStack = info && info.componentStack ? info.componentStack : null;
|
|
const errorMessage = typeof message === 'undefined' ? (error || '').toString() : message;
|
|
const errorDescription = typeof description === 'undefined' ? componentStack : description;
|
|
if (error) {
|
|
return (
|
|
<Alert type="error" message={errorMessage} description={<pre>{errorDescription}</pre>} />
|
|
);
|
|
}
|
|
return children;
|
|
}
|
|
}
|