import React from "react" class ErrorBounDAry extends React.Component{ constructor(props){ super(props) this.state={ hasError:false } } stAIc getDerivedStateFromError(){ return { hasError:true} } componentDidCatch(err,info){ console.error(err,info) } render(){ if(this.state.hasError){ return <div>Oops,err</div> } return this.PRops.children } } // APP.JSX import React from "react" import ErrorBoundary from "./components/ErrorBoundary" import ComponentA from "./components/ComponentA" Export class app extends React.Component{ render(){ return ( <ErrorBoundary> <ComponentA></ComponentA> </ErrorBoundary> ) } }
错误边界无法捕获自身的错误,也无法捕获事件处理、异步代码(setTimeout、requestAnimationFrame)、服务端渲染的错误。


网友回答文明上网理性发言 已有0人参与
发表评论: