Reactjs 错误处理后如何刷新反应页面
我有以下资料:Reactjs 错误处理后如何刷新反应页面,reactjs,Reactjs,我有以下资料: <ErrorBoundary> <NavBar /> </ErrorBoundary> <ErrorBoundary> <Switch> <Route exact path="/" component={mainPage} /> <Route exact path="/" component=
<ErrorBoundary>
<NavBar />
</ErrorBoundary>
<ErrorBoundary>
<Switch>
<Route exact path="/" component={mainPage} />
<Route exact path="/" component={buyerPage} />
<Route exact path="/" component={sellerPage} />
</Switch>
</ErrorBoundary>
<ErrorBoundary>
<Footer />
</ErrorBoundary>
错误发生在buyerPage中:http://localhost:3000/#/buyer. 但是导航栏和页脚组件仍然加载(我想要的)。当我从导航栏点击卖家时,页面链接会转到http://localhost:3000/#/seller 但是错误仍然会出现,除非我手动刷新页面(然后它会显示卖家页面的内容)
当我们点击离开错误页面(在我的例子中是买方页面)时,有没有一种方法可以自动刷新页面
这是我的错误边界代码:
render() {
if(this.state.hasError){
return (
<div style={errorStyle}>
<h2>Something went wrong</h2>
</div>
)
}
else{
//refers to the component we are actually rendering
return (this.props.children);
}
}
render(){
if(this.state.hasError){
返回(
出了点问题
)
}
否则{
//指我们实际渲染的组件
返回(本.道具.儿童);
}
}
在错误边界内
清除组件更新时的hasrerror
:
componentDidUpdate(previousProps, previousState) {
if (previousProps.children!==this.props.children)
this.setState({hasError: false});
}