简体   繁体   中英

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. 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.

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.

Currently, using react-router and withRouter.

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

Then in your back button

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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