Next.js Discord

Discord Forum

Accessing variable from another file in dynamic routing

Answered
QBit posted this in #help-forum
Open in Discord
I have a nextjs app. I have this code in my app/problems/[problemID]/page.tsx
type Props = { params: { problemID: string} }

I want to use this "problemID" variable in @components/custom/excalidraw-wrapper.tsx file. What to do?

15 Replies

Madeiran sardinella
Hi, is excalidraw-wrapper being rendered on that page? You can simply pass it from page to component by props
if it's already a client component: useParams
if it's a server component: simply pass the value as prop
can someone provide me the demo code?
any reference
Answer
Example taken from the doc page
'use client'
 
import { useParams } from 'next/navigation'
 
export default function ExampleClientComponent() {
  const params = useParams<{ tag: string; item: string }>()
 
  // Route -> /shop/[tag]/[item]
  // URL -> /shop/shoes/nike-air-max-97
  // `params` -> { tag: 'shoes', item: 'nike-air-max-97' }
  console.log(params)
 
  return <></>
}
Your case would be const { problemId } = useParams()
okay, let me try
thank you!!