Next.js Discord

Discord Forum

how to retrieve params from dynamic route

Unanswered
Oriental posted this in #help-forum
Open in Discord
OrientalOP
I'm using router.push to create a dynamic URL:
router.push(`/view-page/${candidate.static_id}/${candidate_id}/${candidate.name_id}

To the page that this routes to how do I get the stuff from the URL

28 Replies

OrientalOP
Also for context I will be using the paramaters in a server component. I can't use useParams as that only works inside a client component
Masai Lion
import { useRouter } from 'next/router';
import { useEffect } from 'react';

function ViewPage() {
  const router = useRouter();
  const { static_id, candidate_id, name_id } = router.query;

  useEffect(() => {
    // Do something with the retrieved params
    console.log(static_id, candidate_id, name_id);
  }, [static_id, candidate_id, name_id]);

  return (
    <div>
      {/*your little funny stuff you want to render */}
    </div>
  );
}

export default ViewPage;


useRouter is imported from 'next/router' to access the router object.

router.query contains the dynamic parameters from the URL.
(You can then access these parameters directly like router.query.static_id, router.query.candidate_id, and router.query.name_id)

Finally we use useEffect hook to perform any actions when these parameters change.
and your return can look something like this:

return (
    <div>
      <p>Static ID: {static_id}</p>
      <p>Candidate ID: {candidate_id}</p>
      <p>Name ID: {name_id}</p>
    </div>
  );
OrientalOP
I get this error:
You have a Server Component that imports next/router. Use next/navigation instead.
@Oriental I get this error: You have a Server Component that imports next/router. Use next/navigation instead.
Masai Lion
omggg true, sorry. I still love next/router
try changing it to next/navigation
and then test again
OrientalOP
Error is gone now but also I should mention that the server page needs to get the params so that it can call a few backend functions and then pass the data is retrieves to a client component. Basically like pre-loading the data so it loads instantly
I think it should work I just made a async function called getData
just getting this error now:
Property 'query' does not exist on type 'AppRouterInstance'.ts(2339)
give me a second
OrientalOP
i think its cuz router needs to be imported from next/router
not navigation
@Oriental i think its cuz router needs to be imported from next/router
Masai Lion
Can you show me your code?
OrientalOP
export async function getData() {
    const database = createServerDatabaseClient();

    const router = useRouter();
    const { static_id, candidate_id, name } = router.query;

    const selectedName= await getName(database, name_id);
    if (!selectedName) {
      console.error(`Name with id ${name_id} not found`);
      return { props: { filteredOption: null } };
    }
    const options = await getOptions(database, selectedName);   
    const filteredOption = 
//logic for filtering
    return { props: { filteredOption } };
  }
Next step for me is to show filteredOption on the client component, ideally by passing it as a prop
OrientalOP
it also says that Property 'query' does not exist on type 'AppRouterInstance'.ts(2339)
OrientalOP
@Masai Lion
@Oriental it also says that Property 'query' does not exist on type 'AppRouterInstance'.ts(2339)
Masai Lion
hello, sorry. I don’t really have a clear idea of what we can do. Maybe we can wait until my brain starts working or any of my fellow colleagues helps you out C:
OrientalOP
welp
Wool sower gall maker
Did u try searchParams?
Show me your page.tsx of your dynamic route
@Wool sower gall maker Did u try searchParams?
OrientalOP
searchParams only works with client component
this is a server component
@Oriental this is a server component
Masai Lion
lmao that’s why useRouter doesn’t work
I should have asked before
useRouter is only for client components