reactjs - 在反应路由器上找不到处理资源
问题描述
我有以下路由模式:
<Router history={history}>
<Route path="/" exact render={DashboardPage}/>
<Route path="/accounts/:id" exact render={AccountPage} />
</Router>
const AccountPage = (props) => {
const {match: {params}} = props;
const id = _.toInteger(params.id);
return (
<Layout>
<AccountComponent id={id}/>
</Layout>
)
};
我已经在商店中拥有所有现有帐户,因此无需进行 Ajax 调用来确认存在。我的问题是:如何处理 id 与任何现有资源不匹配的情况?
解决方案
您必须检查componentDidMount
AccountPage 中的 id 是否存在,因此如果 id 不存在,它应该重定向到其他地方或向他显示另一个内容,因此您的代码应该是这样的:
export default class AccountPage extends Component {
componentDidMount(){
const {match: {params}} = props;
const id = _.toInteger(params.id);
//existenceCheck is a function that will check the id existence
if(existenceCheck(id)) {
this.props.history.push('/not-found')
}
}
//render and etc
}
推荐阅读
- c# - 在具有 C# .NET 核心的 Linux 环境下以编程方式提升权限
- android - 应用程序被杀死并处于优化电池模式时未收到 FCM 消息| 一加 5t |一加 6 | 错误广播意图回调:result=CANCELLED
- java - Active PooledConnectionFactory 池连接与会话
- git - Gitlab 合并请求包括来自目标分支的提交
- arrays - 为什么我不能将元素附加到结构?
- json - 提供来自一个实体的多个 Json 响应
- python - SMB 网络共享上的 Python 文件复制
- python - 在 python 中使用 K-means 聚类从简单的 CAPTCHA 进行字母分割
- python - 在 python 脚本中导入 Ironpython
- weblogic - 由于缺少批处理文件,无法启动 appserver