首页 > 解决方案 > 基于Ajax响应的Reactjs动态页面如何处理404?

问题描述

我用动态 ID 反应组件。

<Route path="/dashboard/tag/:id/" component={TagDetail}/>
<Route path="" component={NotFound}/>

TagDetail组件中,我基于返回数据调用服务器ID,但是如果服务器返回ID不存在于服务器(404 状态码),所以我想重定向到NotFound组件。

在 TagDetail 组件中

fetch("URL", {'id": this.props.match.params.id})
     .then(response => response.json()}
     .then(data => {
           this.setState({data})
       })

它在ID正确时起作用,因此服务器返回数据,但如果不是,则无法在其上显示 404 页面。

标签: reactjsreact-routerreact-router-dom

解决方案


推荐阅读