0

I'm following the react js tutorial, and I keep running into this issue

import React from "react";
import NewsCard from "../NewsCard/NewsCard";
const NewsCards = ({ articles }) => {
  return (
    <div>
      {articles.map((article, i) => {
        <NewsCard />;
      })}
    </div>
  );
};

export default NewsCards;
2
  • As the error states, articles is undefined. Which means no such value was provided for this component. Can you provide an example which demonstrates otherwise? Please include a minimal reproducible example demonstrating the problem and indicate specifically where you are supplying a value to articles and why it shouldn't be undefined. Commented Jul 28, 2021 at 11:51
  • Several people have solved this in the answers below, On another note, could you provide the component which renders the NewsCards component? If it is working in the tutorial but not for you, it might be a problem in how you provide the articles props to the NewsCards component. Commented Jul 28, 2021 at 12:01

4 Answers 4

1

Seems like your articles does not have default value as []. You can change as follow. And you should give key attribute when using map function.

const NewsCards = ({ articles }) => {
  const data = articles ? articles : []
  return (
    <div>
      {data.map((article, i) => {
        <NewsCard key={article.id}/>;
      })}
    </div>
  );
};
Sign up to request clarification or add additional context in comments.

Comments

1

If you still need help, just like what the previous answers said, make sure that articles is initialized/defined by using the && operator to make that check. Also, based upon what you wrote, the map method is returning undefined since you specified a function body (using the function body bracket notation {} ) without a return statement. So instead write the map method like this:

<div>
{articles && articles.map((article, i) => <NewsCard />)}
</div> 

or like this:

<div>
{articles && articles.map((article, i) => {
   return <NewsCard />
})}
</div>

The first example has a implicit return since an arrow function is being used and a function body is not present (there are no function body brackets { }).

Comments

0

Probably articles is not initialized when you try to map throught it. Try this:

{articles?.map((article, i) => {
        <NewsCard />;
      })}

OR

{articles && articles.map((article, i) => {
        <NewsCard />;
      })}
    </div>

That way you will first make sure if articles exist

1 Comment

This won't render as the callback inside the map is not returning the component
0

This means that the articles prop is undefined.

There are several ways to solve this. The first and easiest way is by implementing the following logic:

{articles?.length ? articles.map((article, i) => <NewsCard />) : "There are no articles here."}

Another way to solve this is by implementing React proptypes - you can read about this here.

Third and "hardest" (but probably best) way to solve this is by using a static type checking tool. Flow comes to mind, but you can use TypeScript too.

2 Comments

Under the same runtime conditions, this code would produce the error: "Cannot read property 'length' of undefined."
Ah yes, I did not add a question mark before the dot. Will edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.