我的提取功能有时返回错误。如果发生这种情况,我想转到url / notfound并渲染NotFound组件。可以用React Router完成吗?还有其他方法可以从功能更改路由吗?
答案 0 :(得分:1)
您可以显示类似的内容
我正在展示ES6示例
假设您有一个状态变量,该状态变量会根据loadError
个运算结果来更新fetch
this.state={
loadError:false
}
就像,
fetch(this.host+this.url,{method:"GET",redirect:"follow"}).then(()=>
this.setState({loadError:true})
)
使用render()
方法的某个地方
{loadError ? <Redirect to="/NotFound"/>}