react 懒加载和错误边界

import React, {Component,lazy, Suspense} from 'react'
const About = lazy(() => import(/*webpackChunkName: 'about'*/'./child.jsx'))
class Layout extends Component {
  state = {
    error: false
  }
  static getDerivedStateFromError () {
    return {
      error: true
    }
  }
  render () {
    if (this.state.error) {
      return <div>error</div>
    }
    return (
      <div>
        home
        <Suspense fallback={<div>加载中</div>}>
          <About />
        </Suspense>
      </div>
    )
  }
}

 

posted @ 2021-03-29 15:52  异地大光码  阅读(95)  评论(0编辑  收藏  举报