Skip to content

Commit

Permalink
fix(core): always treat error boundary fallback as a callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 25, 2022
1 parent cd21a31 commit 3ecd175
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
10 changes: 5 additions & 5 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ declare module '@theme/Error' {
import type {ComponentProps} from 'react';
import type ErrorBoundary from '@docusaurus/ErrorBoundary';

type ErrorProps = ComponentProps<
type ErrorProps = Parameters<
NonNullable<ComponentProps<typeof ErrorBoundary>['fallback']>
>;
>[0];

export interface Props extends ErrorProps {}
export default function Error(props: Props): JSX.Element;
Expand Down Expand Up @@ -125,13 +125,13 @@ declare module '@docusaurus/constants' {
}

declare module '@docusaurus/ErrorBoundary' {
import type {ReactNode, ComponentType} from 'react';
import type {ReactNode} from 'react';

export interface Props {
readonly fallback?: ComponentType<{
readonly fallback?: (props: {
readonly error: Error;
readonly tryAgain: () => void;
}>;
}) => JSX.Element;
readonly children: ReactNode;
}
export default function ErrorBoundary(props: Props): JSX.Element;
Expand Down
10 changes: 6 additions & 4 deletions packages/docusaurus/src/client/exports/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export default class ErrorBoundary extends React.Component<Props, State> {
const {error} = this.state;

if (error) {
const Fallback = this.props.fallback ?? DefaultFallback;
return (
<Fallback error={error} tryAgain={() => this.setState({error: null})} />
);
const fallback =
this.props.fallback ?? ((props) => <DefaultFallback {...props} />);
return fallback({
error,
tryAgain: () => this.setState({error: null}),
});
}

// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
Expand Down
2 changes: 1 addition & 1 deletion website/docs/docusaurus-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ This component doesn't catch build-time errors and only protects against client-

#### Props {#errorboundary-props}

- `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.
- `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. If not present, `@theme/Error` will be rendered instead.

### `<Head/>` {#head}

Expand Down

0 comments on commit 3ecd175

Please sign in to comment.