ErrorBoundary.tsx 1.3 KB
Newer Older
贾浩@五瓣科技's avatar
贾浩@五瓣科技 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
import type { NextRouter } from 'next/router';
import { useRouter } from 'next/router';
import React from 'react';

interface Props {
  children: React.ReactNode;
  renderErrorScreen: (error?: Error) => React.ReactNode;
  onError?: (error: Error) => void;
}

interface PropsWithRouter extends Props {
  router: NextRouter;
}

interface State {
  hasError: boolean;
  error?: Error;
  errorPathname?: string;
}

class ErrorBoundary extends React.PureComponent<PropsWithRouter, State> {
  state: State = {
    hasError: false,
  };

  static getDerivedStateFromProps(props: PropsWithRouter, state: State) {
    if (state.hasError && state.errorPathname) {
      if (props.router.pathname !== state.errorPathname) {
        return { hasError: false, error: undefined, errorPathname: undefined };
      }
    }

    return null;
  }

  componentDidCatch(error: Error) {
    this.setState({ hasError: true, error, errorPathname: this.props.router.pathname });
    this.props.onError?.(error);
  }

  render() {
    if (this.state.hasError) {
      return this.props.renderErrorScreen(this.state.error);
    }

    return this.props.children;
  }
}

const WrappedErrorBoundary = (props: Props) => {
  const router = useRouter();
  return <ErrorBoundary { ...props } router={ router }/>;
};

export default React.memo(WrappedErrorBoundary);