1

I've mapped my redux store to props, but I'm now having trouble rendering it dynamically. I've also tried projectCards(){...} syntax, but that was a total shot in the dark. My console logs are showing the objects coming through the way I want them. I also tried using projects.map, but I don't think I want to put the return values in a new array. I just want more <Card/> items on the page, dynamically rendered. Where have I gone wrong? Any help would be appreciated.

Class Projects extends...
.
.
.
 projectCards = () => {
  if ( this.props.projects.length !== 0 ) {
   this.props.projects.forEach((project) => {
     return <Card fluid color='green' header={project.name} />
  })
 }
}

 render(){
  return(
    <Container>
      <br/>
      <Card.Group>
        <Card fluid color='green' header='Option 1' />
        <Card fluid color='blue' header='Option 2' />
        <Card fluid color='red' header='Option 3' />
        { this.projectCards() }
      </Card.Group>
    </Container>
  )
 }
}
1

1 Answer 1

1

Try this:

projectCards = () => {
  if ( this.props.projects.length !== 0 ) {
    return this.props.projects.map( project => 
       <Card fluid color='green' header={project.name} />
    )
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It worked! So I wanted to return an array of <Card>s?
Got it! However, do you think that it's possible to make the function return value more than one time? After returning the first Card component, it's done! The other Card components won't have a chance to come to life. Btw, why don't you want to return an array of <Card>s
That's a good point. Not the first time I've tried multiple return statements in the same function today!
I can't edit it, but there's a curly missing in your post. Thanks for your help.

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.