Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): make error boundary fallback a component instead of a callback #7368

Merged
merged 1 commit into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ declare module '@theme-original/*';
declare module '@theme-init/*';

declare module '@theme/Error' {
export interface Props {
readonly error: Error;
readonly tryAgain: () => void;
}
import type {ComponentProps} from 'react';
import type ErrorBoundary from '@docusaurus/ErrorBoundary';

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

export interface Props extends ErrorProps {}
export default function Error(props: Props): JSX.Element;
}

Expand Down Expand Up @@ -119,11 +123,13 @@ declare module '@docusaurus/constants' {
}

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

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

if (error) {
const fallback = this.props.fallback ?? DefaultFallback;
return fallback({
error,
tryAgain: () => this.setState({error: null}),
});
const Fallback = this.props.fallback ?? DefaultFallback;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this fix is in place because we had users who swizzled @theme/Error and used a useState in it, and that broke, because we were calling the component here (!)

I'm fine to change ?? DefaultFallback to ?? (props) => <DefaultFallback {...props} />.

return (
<Fallback error={error} tryAgain={() => this.setState({error: null})} />
);
}

return (
children ??
// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
null
);
// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
return children ?? null;
}
}
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`: 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}

Expand Down