Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Need help to pass data from page to dynamic page

I'm creating a website to showcase products using NextJs. I need to pass data from a page to a dynamic page. All the data is a json array of objects located in a data folder inside the project. I want to list all objects in pages/rolls/index.jsx, and when an objects corresponding "Learn More" Link button is clicked, takes me to a more detailed version of the page. I can't make it work using next/link and useRouter, so here's what I did. My project structure is:

- data
  - rollData.json
- pages
  - rolls
    - [rolldetail].jsx
    - index.jsx
// rollData.json

[
    {
        "name": "8\" x 0.080\" x 300'",
        "price": 300,
        "product_id": "PL8080300",
        "image": "/8flat.webp",
        "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
    },
    {
        "name": "12\" x 0.080\" x 300'",
        "price": 450,
        "product_id": "PL12080300",
        "image": "/12flat.webp",
        "description": "Ac turpis egestas sed tempus urna et pharetra"
    }
]
// rolls.jsx

import rollsdata from '../../data/rollData.json'

const Rolls = () => {
  return (
    <main>
      {rolls.map((roll) => {
          return (
            <div key={roll.product_id}>

              <h2>{roll.name}</h2>

                <h3>
                  Description
                </h3>
                <p>{`${roll.description.slice(0, 163)}.`}</p>

              <div>
                <span>Price</span>
                <span>{`$${roll.price.toFixed(
                  2
                )}`}</span>
              </div>

                <Link href={`/rolls/${encodeURIComponent(roll.product_id)}`}>
                  <a>click</a>
                </Link>

            </div>
          )
        })}
    </main>
  )
}
// [rollDetail].js
import { useRouter } from 'next/router'
import rolls from '../../data/rolls.json'

const RollDetail = () => {
  const router = useRouter()

  const roll = rolls[router.query.rolldetail]

  console.log(roll)
  return (
    <div>
      <h2>Roll detail</h2>
      <p>{roll.name}</p>
      <p>{roll.description}</p>
    </div>
  )
}

export default RollDetail

And it gives me the error:

Uncaught TypeError: roll is undefined

I don't understand how to use router to display a specific roll object, can someone tell me what I'm doing wrong and how to achieve it please? Should I be using getStaticProps and getStaticRoute?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda