Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove one specific query parameter from URL in Next.js?

http://localhost:3000/?search=test&type=abc&category=xyz

After I search for test (among with type and category), the URL changes to the URL above.

return router.push(
      {
         pathname: `/`,
         query: {
             // something to do here...
         },
      },
      "",
      { scroll: false }
);

Next, I have an onClick with this router.push.

I would like to remove ONLY the search query and keep the type and category queries. How is it possible? The documentation doesn't mention anything about this.

like image 444
David Avatar asked Oct 22 '25 11:10

David


1 Answers

To remove the query param completely from the url:

const { paramToRemove, ...routerQuery } = router.query;
router.replace({
  query: { ...routerQuery },
});
like image 97
Jan Swart Avatar answered Oct 25 '25 02:10

Jan Swart