javascript - 在 React Router 中,我如何删除/更改我之前发送的 history.push 中的参数?
问题描述
我正在使用 react-router-dom 版本 4.3.1,并且我正在将参数传递给另一条路线,一切正常,但现在我必须更改/删除该参数,我该怎么做?
我试过这个
this.props.location.state.deleted = false
//admin profile page
history.push('/home/admins', { state: { deleted: true } });
//all admins page
componentDidMount(){
const checkDelete = this.props.location.state.deleted // true
}
基本上我正在从他/她的个人资料页面中删除管理员,然后导航到所有管理员页面,在此页面中我想显示删除管理员的成功消息。
所以我和真正的喜欢一起工作,
componentDidMount(){
const checkDelete = this.props.location.state.deleted // true
if(checkDelete){
this.setState({
messageAlert:true
})
}
}
并且警报将自动关闭,但 checkDelete 始终为真,我如何使其删除或使其为假。
解决方案
const state = { ...history.location.state };
delete state.deleted;
history.replace({ ...history.location, state });
在隐藏消息警报成功时使用上面的代码片段。
推荐阅读
- python - 如何在 TKinter Entry 中输入数组?
- python - Python Pandas:在保留重复的同时加入表
- java - 如何在 Android 中将 PDF 文件转换为 WebP?
- c++ - 如何跨不同视口移植我的应用程序
- vue.js - 出现错误时如何突出显示 vee-validate 表单向导的输入字段?
- python-3.x - Python 写入 csv 文件:io.UnsupportedOperation: not writable
- javascript - Javascript/jQuery 使用随机数组键获取子数组值
- expression - 精确值计数器
- python - 如何以矢量化的方式从 spacy 的管道中获取文本嵌入?
- react-native - 我使用 npx 安装了 react-native。当我运行 npx react-native run-android 时,它给了我以下错误。IT 打开 AVD 但未显示应用程序