本文介绍了NEXT JS - 如何删除查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Next JS (React) 中不刷新页面的情况下删除或更新查询参数?

How can I remove or update query params without refreshing the page in Next JS (React)?

  1. 用户位于 URL /about?login=success&something=yes
  2. 单击按钮并从 URL 中删除 ?login=success&something=yes 而不刷新页面.点击按钮后的 URL 将是 /about
  1. The user is on the URL /about?login=success&something=yes
  2. Click a button and removes ?login=success&something=yes from the URL without refreshing the page. The URL after clicking the button will be /about

我怎样才能实现它?

正如在这个线程中提到的,我知道可以使用路由器删除查询参数或查询字符串.但是,useLocationuseHistorynext/router 上不可用.

As mentioned in this thread, I know that is possible to remove query params or query strings with Router. But, useLocation and useHistory are not avaliable on next/router.

推荐答案

根据 History,你可以使用 history.replaceState 来实现.

According to the History, you can using history.replaceState to implement this.

window.history.replaceState(null, '', '/about')

这篇关于NEXT JS - 如何删除查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:36