简体   繁体   English

有没有一种方法可以更改URL而无需重新加载或更改历史记录?

[英]Is there a method to change the URL without reloading or making changes to the history?

I have a react app where the user clicks a button and then a random item is returned, and I want to take a property off that item and attach it to the URL. 我有一个React应用程序,用户单击一个按钮,然后返回一个随机项目,我想从该项目中删除一个属性并将其附加到URL。 However, I do not want the page to reload. 但是,我不希望页面重新加载。 While I can use the history.replace / history.push to update the URL, the problem is that if the user clicks back then they will be returned to the last page, which is correctly rendered based off the previous values in the redux-store, yet the URL will lag behind. 虽然我可以使用history.replace / history.push来更新URL,但问题是,如果用户单击返回,则它们将返回到最后一页,该页面根据redux-store中的先前值正确呈现。 ,但网址会落后。

For example, if the user was on myapp/item6 and hit the back button they would be returned to myapp/item5 yet the URL would not change to reflect this. 例如,如果用户在myapp / item6上并单击“后退”按钮,他们将返回到myapp / item5,但URL不会更改以反映此情况。

Currently, using react-router and withRouter. 当前,使用react-router和withRouter。

<Link 
  to={{ 
    pathname: '/somelocation',
    state: { from: props.location } 
  }}
/>

Then in your back button 然后在您的后退按钮中

<Link to={props.location.state.from}>
  Back
</Link>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM