Next.js Discord

Discord Forum

can someone explain the params thing where u get the number or smthn in dynamic routing bit confused

Unanswered
Black Caiman posted this in #help-forum
Open in Discord
Black CaimanOP
^

5 Replies

Macao paper wasp
What do you want to know about it?
@Macao paper wasp What do you want to know about it?
Black CaimanOP
how to do it
and maybr how to restrict user from entering anything they want
@Black Caiman and maybr how to restrict user from entering anything they want
they always will be able to put anything they want ;) but, you can add logic to check for the [slug](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#convention) to ensure it's rendering what you intended by, for example, using the slug to fetch from an external API:

// app/profile/[slug]/page.tsx

type Props = {
  params: Promise<{
    slug: string;
  }>;
}

export default fnuction ProfilePage({ params }: Props) {
  const slug = (await params).slug;
  
  // Fetch from an API (example)
  const response = await fetch(`https://api.something.com/profile/${slug}`, {
    // ...
  });
  // ...

  const { profile } = await res.json();

  if (!profile) {
    // Return JSX 
    // return <h1>Invalid</h1>
    
    // Return redirect
    // import { redirect } from 'next/navigation'
    // return redirect("/")
  }

  return <ProfileComponent profile={profile} />
}

this is just one example of a way to validate the route(s)