Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router Dom - Clear url parameters without refreshing the page

I do have some parameters passed by a link to an url, like :

https://mysite/post/list?success

Considering I'm with React JS and using React router dom, how should I do to clean the parameters without refreshing the page, in order to be back button friendly ? I'm aiming for the same page, without params.

Edit : Proposed here, this solution does not work for this case

  const queryParams = new URLSearchParams(location.search)
  queryParams.delete('success')
  history.replace({
    search: queryParams.toString(),
  })
    

It does work while treating params case to case (by applying "if" chains), but I'm searching for a function clearing them all at once - is there a way to do that with the .delete method ?

like image 803
Horkos Avatar asked Nov 16 '25 03:11

Horkos


1 Answers

Found it !

   const queryParams = ""
   history.replace({
     search: queryParams,
   })

'Twas dead simple and efficient. Closing.

like image 182
Horkos Avatar answered Nov 17 '25 17:11

Horkos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!