最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove a query string using react-router without reloading page? - Stack Overflow

programmeradmin0浏览0评论

In my application I have a query string that I want to remove after a button click.

I get the query string like this:

  import qs from 'query-string'
  // ..

  const { search } = url
  const { id } = qs.parse(search)

Then when I click on the button, I want to remove the string without reloading the page.

  const onClick = () => {
     history.push({})
  }

However the code above doesn't work as expected.

In my application I have a query string that I want to remove after a button click.

I get the query string like this:

  import qs from 'query-string'
  // ..

  const { search } = url
  const { id } = qs.parse(search)

Then when I click on the button, I want to remove the string without reloading the page.

  const onClick = () => {
     history.push({})
  }

However the code above doesn't work as expected.

Share Improve this question asked Apr 28, 2020 at 10:04 RaedRaed 6981 gold badge7 silver badges24 bronze badges 1
  • Did you solved your problem? – Samuel Vaillant Commented Apr 29, 2020 at 14:39
Add a ment  | 

1 Answer 1

Reset to default 8

You should be able to replace the state with a call to replace. The call to push will add a new entry to the history which break the previous button. The call to replace don't push but replace the current page.

const onClick = () => {
  history.replace(pathWithoutTheQuery)
}
发布评论

评论列表(0)

  1. 暂无评论