// 使用 HOC 以方便为各个页面复用这段逻辑
function createErrorBoundary(Page) {
  return class ErrorBoundary extends Component {
    el = React.createRef();
    state = {
      hasError: null,
    };

    static getDerivedStateFromError() {
      return {
        hasError: true,
      };
    }

    componentDidCatch(error, errorInfo) {
      console.log(error, errorInfo);
    }

    /* Start 需要手动调用页面组件上的生命周期方法 **/
    componentDidShow() {
      return this.el.current?.componentDidShow?.();
    }

    componentDidHide() {
      return this.el.current?.componentDidHide?.();
    }

    onShareAppMessage() {
      return this.el.current?.onShareAppMessage?.();
    }

    //...

    /* End 需要手动调用页面组件上的生命周期方法 **/

    render() {
      return this.state.hasError ? (
        <View>Something went wrong.</View>
      ) : (
        <Page ref={this.el} />
      );
    }
  };
}

export default createErrorBoundary;
