To retrieve the query
and pathname
, you will need to utilize the functions useSearchParams
and usePathname
. If you wish to update an existing history record instead of adding a new one, you can use router.replace
.
'use client'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
export default function Page() {
const pathname = usePathname()
const searchParams = useSearchParams()
const router = useRouter()
const updateQuery = () => {
const newUrlParams = new URLSearchParams(searchParams.toString())
newUrlParams.set('filters', ['219', '213'].join(','))
router.push(`${pathname}?${newUrlParams}`)
}
return (
<div>
<button onClick={updateQuery}>Update filter</button>
</div>
)
}