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

130
Visualizações
displaying array data in react. output is not coming

when I am printing console.log(response.data) in console below output is coming.

enter image description here

I created one hook const [result,setResult] = useState([]);

and then output of API I set as

setResult(response.data);

when I am writing below code it is just printing " Data is". it is not printing output of result

              <h1>
                  Data is
               {
                   result.map((res:any)=>{
                       {res.id};
                   })
                }    

              </h1>  

what mistake I am doing?

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

0

The whole logic of your code it's okay. You are using the useState Hook correctly. And the data your passing to the state is an array which will work fine with the map() method. The only problem I'm seeing is that your map() method is not well implemented.

Try this instead:

<h1>
  Data is
  {
   result.map((res:any)=>{
      return <p>{res.id}</p>
   })
  }    
</h1>  

The map() method always returns something. When it is a one liner it is not necessary to type return.

<h1>
  Data is
  {
   result.map((res:any) => res.id )
  }    
</h1>  

To print multiple properties of the object:

<h1>
  Data is
  {
   result.map((res:any) => {
      return [
        <div key={res.id}>
           <p>{res.id}</p>
           <p>{res.name}</p>
           <p>{res.createdAt}</p>
        </div>
      ]
   })
  }    
</h1>  
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