最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - React Router Dom - Clear url parameters without refreshing the page - Stack Overflow

matteradmin5PV0评论

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 ?

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 ?

Share Improve this question edited Jul 3, 2021 at 16:30 Horkos asked Jul 3, 2021 at 16:05 HorkosHorkos 4012 gold badges9 silver badges22 bronze badges 2
  • 1 Does this answer your question? How to remove query param with react hooks? – gantoine Commented Jul 3, 2021 at 16:11
  • Unfortunately not, I'd try to clear the whole params and not a single and defined string, so the .delete() is not doing its work... – Horkos Commented Jul 3, 2021 at 16:22
Add a ment  | 

1 Answer 1

Reset to default 3

Found it !

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

'Twas dead simple and efficient. Closing.

Post a comment

comment list (0)

  1. No comments so far