1

I have a simple method which returns jsx that looks as follows:

renderNewsItems = newsItems => {
    return(
        newsItems.map(newsItem => 
        <NewsItem title={newsItem.title} imageUrl={newsItem.imageUrl} />)
    );
  }

and this works fine however when I try to add markup to layout the individual <NewsItem /> components, I'm struggling to figure out how to get it to work. My current, broken attempt looks as follows:

renderNewsItems = newsItems => {
    return(
        <div className="row">
            newsItems.map(newsItem => 
            <div className="col-4">
                <NewsItem title={newsItem.title} imageUrl={newsItem.imageUrl} />
            </div>
        </div>
    );
  }

which results in the error:

'newsItem' is not defined no-undef

In case it's not clear, I'm trying to use the bootstrap grid system to set the layout for how my individual <NewsItem/> components should be rendered

1
  • You're not returning anything when mapping... Commented Feb 19, 2019 at 19:57

1 Answer 1

3

You were close. Write it:

renderNewsItems = newsItems => {
  return (
    <div className="row">
      {newsItems.map(newsItem => (
        <div className="col-4">
          <NewsItem
            key={newsItem.id}
            title={newsItem.title}
            imageUrl={newsItem.imageUrl}
          />
        </div>
      ))}
    </div>
  );
};

Note: I assume you have id property on newsItem. If you don't have, you can use title too as key value, until they are uniq across the all the array elements.

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

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.