reactjs - react-router-dom,如何跳过后退按钮
问题描述
单击时,我有一个将用户导航到另一个页面的项目列表。
问题是目标页面需要某种在此列表项页面中不存在的 id。所以我这样做:
列表页面:
{items.map(item=>{
return <Link to="/resolver/${item}">{item}</Link>
})
解析器页面:
componentDidmout(){
const {keyword} = this.props.match.params;
this.props.searchOnServer(keyword)
}
componentDidUpdate(pervProps){
//after checking some conditions:
const {redirect_id} = this.props.server_response;
this.props.history.push(`/some/path/${redirect_id}`)
}
然后目标页面可以使用该 id 来获取产品相关数据:
componentDidMount(){
const {id} = this.props.match.params;
fetchSomeStuff(id);
}
现在,上述解决方案完全正常,我唯一的问题是当用户点击目标页面中的后退按钮时,它会返回到解析器页面,整个循环重新开始。如何跳过解析器页面并返回列表页面。
解决方案
您可以从解析器页面进行重定向,因此当用户从新页面点击后退按钮时,他们将返回到解析器路径之前的页面。
更改history.push
为history.replace
。
componentDidUpdate(pervProps){
//after checking some conditions:
const {redirect_id} = this.props.server_response;
this.props.history.replace(`/some/path/${redirect_id}`)
}
推荐阅读
- c# - 团队构建期间设置的 wixtool 获取 light.exe System.UnauthorizedAccessException:访问路径
- grafana - 如何将视频/流添加到 Grafana 仪表板?
- android - webview的iframe下拉刷新
- android - 已发送的 Gmail 邮件弹出窗口
- css - 文本动画在 Safari 中不起作用
- postgresql - sqlalchemy 创建临时表
- raspberry-pi - dist-upgrade 后自动关闭 raspbian 上的对话框
- python - 如果另一个数组不继续,则通过重复值来扩展数组
- android - ImageView src 将图像移动到中心,在更改宽度和高度时使两侧可见
- html - 将水平线放在两个 div 之间的中间