import React, { Component } from 'react'; import { View, Text } from '@tarojs/components'; interface Props { children: React.ReactNode; } interface State { hasError: boolean; } export default class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(): State { return { hasError: true }; } componentDidCatch(error: Error, info: React.ErrorInfo) { console.error('[ErrorBoundary]', error, info.componentStack); } render() { if (this.state.hasError) { return ( 😵 页面出了点问题 请返回重试 ); } return this.props.children; } }