How can i pass pathname and query in Next js 13.4 to router.push() in App router? #55906
Unanswered
Raghunadh9
asked this question in
App Router
Replies: 3 comments 3 replies
-
you can use import { useRouter} from 'next/navigation'; const router = useRouter() |
Beta Was this translation helpful? Give feedback.
3 replies
-
I had same issues and tried some workarounds, this solution worked for me: import {useRouter} from "next/navigation" const navigate = useRouter(); const query = new URLSearchParams({
|
Beta Was this translation helpful? Give feedback.
0 replies
-
You can write a custom hook to achieve it, here's an example I use in my projects: import { useRouter as useNextRouter } from 'next/navigation'
const useRouter = () => {
const { push: nextPush, ...rest } = useNextRouter()
const customPush = ({
pathname,
query,
}: {
pathname: string
query: Record<string, any>
}) => {
const generateQuery = new URLSearchParams(query).toString()
nextPush(`${pathname}?${generateQuery}`)
}
return { ...rest, push: customPush }
}
export default useRouter |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
My instructor using Next js 11 version, he used to
import router from "next/router"
. but as we know thatnext/router
has been depreciated. he passes pathname and query to therouter.push
like this:How can i do it in App router? We need to update. not degrade. any suggestions?
Beta Was this translation helpful? Give feedback.
All reactions