首页 > 解决方案 > 在 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 始终为真,我如何使其删除或使其为假。

标签: javascriptreactjsbrowserreact-router

解决方案


 const state = { ...history.location.state };
 delete state.deleted;
 history.replace({ ...history.location, state });

在隐藏消息警报成功时使用上面的代码片段。


推荐阅读