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

150
Visualizações
TypeError: Cannot read properties of undefined (reading 'state') in react

I had passed the parameter to other component through Link

{props.posts.map((post)=>{
          return (<>
          
           <tr  >
           <td>{post.title}</td>
           <td class="pull-right"   id="underline"><Link to={{pathname:"/details",state:{title:post.title}}} >Details</Link> </td>
          
       </tr>
       </>)
      })}

But when I tried to extract value of title in my Details component it showed error.
My Details.js is as

import React from 'react'
import { useLocation } from 'react-router-dom';

export default function Details(props) {
    return (
        <div>

            <section class="section">
                <br />
                <section class="details">
                    <h4>Title:{props.location.state.title} </h4>
                    <br />
                    <h4>Categories: </h4><br />
                    <h4>Content: </h4>
                </section>
                <br />
            </section>

        </div>

    )
}

I got the error as

TypeError: Cannot read properties of undefined (reading 'state')

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are trying to access the data from props, whereas you've passed data from the Link component, and not directly to the to-be-rendered component, to resolve this issue, you could make use of the useRouter hook. Firstly create an instance of this hook. Use the created instance to drill through the state object.

If you're using react-router-dom v6 and above, there are few syntactical updates made. path and state needs to be passed as separate props and the state properties need to be wrapped within {}.

{props.posts.map((post)=>{
          return (<>
          
           <tr  >
           <td>{post.title}</td>
           <td class="pull-right" id="underline"><Link to={"/details"} state={{title:post.title}}>Details</Link> </td>
          
       </tr>
       </>)
      })}

MyDetails.js

import React from 'react'
import { useLocation } from 'react-router-dom';

export default function Details(props) {
    const location = useLocation() //Instance of useLocation hook.
    return (
        <div>

            <section class="section">
                <br />
                <section class="details">
                    <h4>Title:{location.state.title} </h4> 
                    <br />
                    <h4>Categories: </h4><br />
                    <h4>Content: </h4>
                </section>
                <br />
            </section>

        </div>

    )
}
about 4 years ago · Juan Pablo Isaza Relatório
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