Add shallow routing option with next/navigation's router #48320
Replies: 5 comments 7 replies
-
|
+1 another work around right now we are leveraging is to only use Layout and having the |
Beta Was this translation helpful? Give feedback.
-
|
In the meantime, I've been using in a |
Beta Was this translation helpful? Give feedback.
-
To achieve this and basically watch the URL changes on components I used the code below: |
Beta Was this translation helpful? Give feedback.
-
|
Up to now, it seems shallow routing option has not been supported in next/navigation's router yet. I have a project named LafTools(laf-tools.com) using next.js, to avoid reloading page, I'm currently using other workaround as below
function push(url: string){ Just FYI, hopefully it makes senses. |
Beta Was this translation helpful? Give feedback.
-
|
Official recommended writing method 'use client'
import { useSearchParams } from 'next/navigation'
export default function SortProducts() {
const searchParams = useSearchParams()
function updateSorting(sortOrder: string) {
const urlSearchParams = new URLSearchParams(searchParams.toString())
urlSearchParams.set('sort', sortOrder)
window.history.pushState(null, '', `?${urlSearchParams.toString()}`)
}
return (
<>
<button onClick={() => updateSorting('asc')}>Sort Ascending</button>
<button onClick={() => updateSorting('desc')}>Sort Descending</button>
</>
)
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Provide an option in next/navigation's router's
pushmethod to allow for shallow routing, similar to Next 12's shallow routing.Background
Currently, the recommended approach to mutate search params is to use
router.pushto update the url. However, this causes the page to reload and all the components to remount. This can have undesirable affects, such as triggering animations or other effects that trigger on mount. If the search params are being updated frequently, say with each keystroke, this can cause a very jarring experience. A{ shallow: true }option like in Next 12's router would allow the consumer to mutate the url without remounting the components.Beta Was this translation helpful? Give feedback.
All reactions