Next.js Discord

Discord Forum

How to navigate to path with param in App directory?

Answered
Rule posted this in #help-forum
Open in Discord
Hi. I'm trying to navigate to a path with param in app directory.

My page file is located: /app/user/[userId]/page.tsx

What is the equivalent to this pages directory code for app directory?
import { useRouter } from "next/router";

const router = useRouter();
const userId = 1;

router.push({ pathname: router.pathname, query: { userId } });

I would prefer not to use syntax like this: router.push("/user/1")

7 Replies

that is the way tho (router.push("/user/1"))
I have a few other routes inside the [userId] directory so I have paths like this:

/user/1/something or /user/1/something-else. I want to be able to update the user id inside those pages while keeping the same route
I mean I could get the current path and modify the string manually but just wondering if there is a correct way to do it
Answer
Yea. I guess there is no support for the previous syntax anymore.
Thanks for the help
@Rule Thanks for the help
no problem, and thats sad that they don't have that (i kinda forgot about that syntax)