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

97
Visualizações
Nothing is showing when I loop through the array using forEach in react

I want to show the blog author and blog title in the array on my html page but nothing is displayed. I used the map method and it worked but the forEach method shows nothing. I will like a solution in for loop and forEach loop Here is my code:

import { useState } from "react";

const Home = () => {

const [blogs, setBlogs] = useState([
    { title: 'My new website', body: 'lorem ipsum...', author: 'mario', id: 1 },
    { title: 'Welcome party!', body: 'lorem ipsum...', author: 'yoshi', id: 2 },
    { title: 'Web dev top tips', body: 'lorem ipsum...', author: 'mario', id: 3 }
]);


return ( 
    <div className="home">
      

         {blogs.forEach((blog) => {
            <div className="blog-preview" key={blog.id}>
                <h2>{ blog.title }</h2>
                <p>Written by{ blog.author }</p>
            </div>
        })} 
       
    </div>
      
 );
 }export default Home;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try it with map.
{blogs.map((blog) => {<div className="blog-preview" key={blog.id}><h2>{ blog.title }</h2><p>Written by{ blog.author }</p></div>})}

about 4 years ago · Juan Pablo Isaza Relatório

0

I usually prefer to use .map for this like

blogs.map((blog) => (
      <div> //your code </div>
    ))
about 4 years ago · Juan Pablo Isaza Relatório

0

Use map instead because the map method returns a new array by applying the callback function on each element of an array, while the forEach method doesn't return anything.

so you have to use it like this.

{blogs.map((blog) => {
            <div className="blog-preview" key={blog.id}>
                <h2>{ blog.title }</h2>
                <p>Written by{ blog.author }</p>
            </div>
        })} 

like this.

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