3

Projects.js

 constructor() {
  super();
  this.state = {
     projects: [] 
  }
}

componentWillMount() {
  this.setState({
     projects: [
        {
           link: '/',
           img: 'img.jpg',
           title: 'Title',
           date: 'Jul 2017',
           description: 'Description',
           icons: 
           [
              {
                 src: 'icon.svg',
                 content: 'Content'
              },
              {
                 src: 'icon2.svg',
                 content: 'Content 2'
           ]
        }
     ]
   });
  }

render() {
   return (
      <section className="projects">
         <ProjectsList projects={this.state.projects} />
      </section>
   );
  }
}

ProjectsList.js

let projectItems;

projectItems = this.props.projects.map(project => {
  return (
    <ProjectItem key={project.title} project={project}  />
  ) 
});

return (
   <div id="projectsList">
     {projectItems}
   </div>
)

I want to create a project card that has a list of icons on the bottom. I can map the projects array but the icons won't work. Anyone know how to map this 2d array in ReactJs? Thanks!

3 Answers 3

3

Simply use map again:

projectItems = this.props.projects.map(project => {
  return (
    <div>
      <ProjectItem key={project.title} project={project}  />
      { 
        project.icons.map(i => <img key={i.src} src={i.src} alt="" />) 
      }
    </div>
  ) 
});

You can do it of course inside ProjectItem

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick answer. I've putted your code there inside ProjectItem but it says that projectItems is undefined. Sry to bother you, i'm a complete Noob in react.
This answer really helped me with the 5 day / 3 hour forecast data using the Open Weather API! Thank you so much!
1
array = [[null,null,null],[null,null,null]]

return (
<div> {array.map(x=> x.map(y=> <div>{....}</div> ))}</div>
)

mapping it again inside the array will give the value

Comments

0

In your ProjectItem component you need to map over the Icons. Your ProjectItem component will look like

const ProjectItem = ({project}) => {
    return (
        <div className={'card'}>
            {/*content for link, img, title, description here */}
             {project.icons.map((icon, key) => <img src={icon.content} />)}
        </div>
    )
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.