mirror of
https://github.com/facebook/docusaurus.git
synced 2025-12-26 01:33:02 +00:00
fix(core): always treat error boundary fallback as a callback (#7492)
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
e955ae472d
commit
9cf2bf1199
|
|
@ -77,14 +77,9 @@ declare module '@theme-original/*';
|
|||
declare module '@theme-init/*';
|
||||
|
||||
declare module '@theme/Error' {
|
||||
import type {ComponentProps} from 'react';
|
||||
import type ErrorBoundary from '@docusaurus/ErrorBoundary';
|
||||
import type {FallbackParams} from '@docusaurus/ErrorBoundary';
|
||||
|
||||
type ErrorProps = ComponentProps<
|
||||
NonNullable<ComponentProps<typeof ErrorBoundary>['fallback']>
|
||||
>;
|
||||
|
||||
export interface Props extends ErrorProps {}
|
||||
export interface Props extends FallbackParams {}
|
||||
export default function Error(props: Props): JSX.Element;
|
||||
}
|
||||
|
||||
|
|
@ -125,13 +120,17 @@ declare module '@docusaurus/constants' {
|
|||
}
|
||||
|
||||
declare module '@docusaurus/ErrorBoundary' {
|
||||
import type {ReactNode, ComponentType} from 'react';
|
||||
import type {ReactNode} from 'react';
|
||||
|
||||
export type FallbackParams = {
|
||||
readonly error: Error;
|
||||
readonly tryAgain: () => void;
|
||||
};
|
||||
|
||||
export type FallbackFunction = (params: FallbackParams) => JSX.Element;
|
||||
|
||||
export interface Props {
|
||||
readonly fallback?: ComponentType<{
|
||||
readonly error: Error;
|
||||
readonly tryAgain: () => void;
|
||||
}>;
|
||||
readonly fallback?: FallbackFunction;
|
||||
readonly children: ReactNode;
|
||||
}
|
||||
export default function ErrorBoundary(props: Props): JSX.Element;
|
||||
|
|
|
|||
|
|
@ -45,7 +45,9 @@ export default function Layout(props: Props): JSX.Element {
|
|||
<Navbar />
|
||||
|
||||
<div className={clsx(ThemeClassNames.wrapper.main, wrapperClassName)}>
|
||||
<ErrorBoundary fallback={ErrorPageContent}>{children}</ErrorBoundary>
|
||||
<ErrorBoundary fallback={(params) => <ErrorPageContent {...params} />}>
|
||||
{children}
|
||||
</ErrorBoundary>
|
||||
</div>
|
||||
|
||||
{!noFooter && <Footer />}
|
||||
|
|
|
|||
|
|
@ -23,14 +23,12 @@ import SiteMetadataDefaults from './SiteMetadataDefaults';
|
|||
// TODO, quick fix for CSS insertion order
|
||||
// eslint-disable-next-line import/order
|
||||
import ErrorBoundary from '@docusaurus/ErrorBoundary';
|
||||
// eslint-disable-next-line import/order
|
||||
import Error from '@theme/Error';
|
||||
|
||||
export default function App(): JSX.Element {
|
||||
const routeElement = renderRoutes(routes);
|
||||
const location = useLocation();
|
||||
return (
|
||||
<ErrorBoundary fallback={Error}>
|
||||
<ErrorBoundary>
|
||||
<DocusaurusContextProvider>
|
||||
<BrowserContextProvider>
|
||||
<Root>
|
||||
|
|
|
|||
|
|
@ -7,13 +7,21 @@
|
|||
|
||||
import React, {type ReactNode} from 'react';
|
||||
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
||||
import DefaultFallback from '@theme/Error';
|
||||
import type {Props} from '@docusaurus/ErrorBoundary';
|
||||
import ThemeError from '@theme/Error';
|
||||
import type {
|
||||
FallbackFunction,
|
||||
FallbackParams,
|
||||
Props,
|
||||
} from '@docusaurus/ErrorBoundary';
|
||||
|
||||
type State = {
|
||||
error: Error | null;
|
||||
};
|
||||
|
||||
const DefaultFallback: FallbackFunction = (params) => (
|
||||
<ThemeError {...params} />
|
||||
);
|
||||
|
||||
export default class ErrorBoundary extends React.Component<Props, State> {
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
|
|
@ -32,10 +40,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 fallbackParams: FallbackParams = {
|
||||
error,
|
||||
tryAgain: () => this.setState({error: null}),
|
||||
};
|
||||
const fallback: FallbackFunction = this.props.fallback ?? DefaultFallback;
|
||||
return fallback(fallbackParams);
|
||||
}
|
||||
|
||||
// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
|
||||
|
|
|
|||
|
|
@ -55,7 +55,13 @@ 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 render callback returning a JSX element. It will receive an object with 2 attributes: `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. `@theme/Error` is used for the error boundaries wrapping the site, above the layout.
|
||||
|
||||
:::caution
|
||||
|
||||
The `fallback` prop is a callback, and **not a React functional component**. You can't use React hooks inside this callback.
|
||||
|
||||
:::
|
||||
|
||||
### `<Head/>` {#head}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue