HocError.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import React, { Component } from 'react'
  2. const xhh = require('@/public/xhh.jpeg')
  3. /**错误边界捕获*/
  4. function HocError(Element: any) {
  5. return class ErrorBoundary extends Component<any> {
  6. constructor(props:any){
  7. super(props)
  8. }
  9. state = { hasError: false }
  10. static getDerivedStateFromError(error: any) {
  11. return { hasError: true }
  12. }
  13. componentDidCatch(...err: any) {
  14. // 你同样可以将错误日志上报给服务器
  15. console.log(...err);
  16. }
  17. render() {
  18. let { hasError } = this.state
  19. if (hasError) {
  20. return <>
  21. <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', flexFlow: 'column', height: '100%', background: '#fff' }}>
  22. <div style={{ fontSize: 25 }}>恭喜你发现了一个BUG,请保护BUG现场,钉钉联系沈武!</div>
  23. <img src={xhh} />
  24. </div>
  25. </>
  26. }
  27. return <Element {...this.props} />
  28. }
  29. }
  30. }
  31. export default HocError