Search code examples
next.jsreplit

I can't render nextjs


I'm working on a nextjs project and I am having an issue rending elements through jsx. This is my code:

{
        this.state.projects.forEach((projects)=>{
          <Project name={projects.name} id={projects.id} url={`/projects/${projects.id}`} description={projects.description} ownerUsername={projects.author.username} ownerImage={projects.author.image}/>
        })
}

The component doesn't render.


Solution

  • You have to return an array for your use case using Array.prototype.map

    {
       this.state.projects.map((project)=> {
             return (<Project
                       key={project.id} // don't forget the key prop
                       name={project.name} 
                       id={project.id} 
                       url={`/projects/${project.id}`} 
                       description={project.description} 
                       ownerUsername={project.author.username} 
                       ownerImage={project.author.image}
                     />
                )
      }
    }