我试图将错误捕获添加到组件的渲染功能。当我在实际的render函数中引发错误时,这种方法很好用,但是如果组件的子代中存在错误,则try不会捕获错误(或者它们被子组件错误处理程序拦截,我不确定?)
无论如何有强迫错误的父母。
const SimpleComponent = React.createClass({
render: function(){
try{
throw 'new error'
return <div>{this.props.children}</div>
}catch(e){
console.log('error', e);
}
}
})
以上作品
const SimpleComponent = React.createClass({
render: function(){
try{
return <div>{this.props.children}</div>
}catch(e){
console.log('error', e);
}
}
})
const ChildComponent = React.createClass({
render: function(){
throw 'child error'
}
})
<SimpleComponent>
<ChildComponent />
</SimpleComponent>
上面没有抓住
使用React 16中的componentDidCatch()方法。
检查此以获取更多信息
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句