mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
fix(core): make error boundary fallback a component instead of a callback (#7368)
This commit is contained in:
parent
77fa3d1470
commit
f29bb73300
3 changed files with 20 additions and 18 deletions
|
@ -75,10 +75,14 @@ declare module '@theme-original/*';
|
||||||
declare module '@theme-init/*';
|
declare module '@theme-init/*';
|
||||||
|
|
||||||
declare module '@theme/Error' {
|
declare module '@theme/Error' {
|
||||||
export interface Props {
|
import type {ComponentProps} from 'react';
|
||||||
readonly error: Error;
|
import type ErrorBoundary from '@docusaurus/ErrorBoundary';
|
||||||
readonly tryAgain: () => void;
|
|
||||||
}
|
type ErrorProps = ComponentProps<
|
||||||
|
NonNullable<ComponentProps<typeof ErrorBoundary>['fallback']>
|
||||||
|
>;
|
||||||
|
|
||||||
|
export interface Props extends ErrorProps {}
|
||||||
export default function Error(props: Props): JSX.Element;
|
export default function Error(props: Props): JSX.Element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,11 +123,13 @@ declare module '@docusaurus/constants' {
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module '@docusaurus/ErrorBoundary' {
|
declare module '@docusaurus/ErrorBoundary' {
|
||||||
import type {ReactNode} from 'react';
|
import type {ReactNode, ComponentType} from 'react';
|
||||||
import type ErrorComponent from '@theme/Error';
|
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
readonly fallback?: typeof ErrorComponent;
|
readonly fallback?: ComponentType<{
|
||||||
|
readonly error: Error;
|
||||||
|
readonly tryAgain: () => void;
|
||||||
|
}>;
|
||||||
readonly children: ReactNode;
|
readonly children: ReactNode;
|
||||||
}
|
}
|
||||||
export default function ErrorBoundary(props: Props): JSX.Element;
|
export default function ErrorBoundary(props: Props): JSX.Element;
|
||||||
|
|
|
@ -33,17 +33,13 @@ export default class ErrorBoundary extends React.Component<Props, State> {
|
||||||
const {error} = this.state;
|
const {error} = this.state;
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
const fallback = this.props.fallback ?? DefaultFallback;
|
const Fallback = this.props.fallback ?? DefaultFallback;
|
||||||
return fallback({
|
|
||||||
error,
|
|
||||||
tryAgain: () => this.setState({error: null}),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
children ??
|
<Fallback error={error} tryAgain={() => this.setState({error: null})} />
|
||||||
// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
|
|
||||||
null
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
|
||||||
|
return children ?? null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ This component doesn't catch build-time errors and only protects against client-
|
||||||
|
|
||||||
#### Props {#errorboundary-props}
|
#### Props {#errorboundary-props}
|
||||||
|
|
||||||
- `fallback`: an optional callback returning a JSX element. It will receive two props: `error`, the error that was caught, and `tryAgain`, a function (`() => void`) callback to reset the error in the component and try rendering it again.
|
- `fallback`: a React component. The error boundary will render the component with two props: `error`, the error that was caught, and `tryAgain`, a function (`() => void`) callback to reset the error in the component and try rendering it again.
|
||||||
|
|
||||||
### `<Head/>` {#head}
|
### `<Head/>` {#head}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue