2

I have Interfaces like this:

interface INewsProps {
  newsType: string;
  data: INewsContentProps[];
}

interface INewsContentProps {
  title: string;
  newsContent: string;
}

My array is this:

  export const newsList: INewsContentProps[] = [
  {
    title: 'Lorem ipsum',
    newsContent: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis hendrerit dui ac accumsan consequat.'
  },
  {
    title: 'Lorem ipsum2',
    newsContent: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis hendrerit dui ac accumsan consequat.'
  }
];

I need to get properties from the array to show here:

export const NewsCard = () => {
  return (
     <div>
        <Card>
          <CardActionArea>
            <CardContent>
              <Typography>
                {props.title} 
              </Typography>
               <Typography>
                {props.newsContent} 
              </Typography>
              <ContextMenu />
            </CardContent>
          </CardActionArea>
        </Card>
     </div>
  );
};

I am new in React and also in TypeScript so I will be very grateful if someone can help me with this.

2
  • Note that JSON is a text format, like XML or CSV. The list you show is just an array of objects. Nothing JSON about it. Commented Mar 15, 2021 at 21:42
  • Does this answer your question? Loop through simple array of objects in React Commented Mar 15, 2021 at 21:46

3 Answers 3

2

Unsure if you want a card per entry in your array but you want to make use of Array's map() function like so:

export const NewsCard = (newsItem) => {
  return (
     <div>
        <Card>
          <CardActionArea>
            <CardContent>
              <Typography>
                {newsItem.title} 
              </Typography>
               <Typography>
                {newsItem.newsContent} 
              </Typography>
              <ContextMenu />
            </CardContent>
          </CardActionArea>
        </Card>
     </div>
  );
};

return newsList.map(newsItem => NewsCard(newsItem));

This will map each element in your array to a <Card> element where each entry in the array is accessed by newsItem. I hope this is what you wanted!

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

2 Comments

Glad this is what you’re looking for - I can’t immediately see why you’re getting that error so I might need more code to aid with that! It sounds like your news card function is not returning any JSX as expected?
Glad you got it working Fresh! Don’t forget to mark the answer as correct and upvote it!
2

To render multiple elements you can use .map() function for arrays. Your NewsCard component should accept an argument as props to access props.title and props.newsContent.

return newsList.map((content, ix) => (
    <NewsCard
      key={ix + content.title}
      title={content.title}
      newsContent={content.newsContent}
    />
  ));

You can refer this working demo on Codesandbox

Comments

2

You can use NewsCard as component. Lets say that

export const NewsCard = (props) => {
  return (
     <div>
        <Card>
          <CardActionArea>
            <CardContent>
              <Typography>
                {props.title} 
              </Typography>
               <Typography>
                {props.newsContent} 
              </Typography>
              <ContextMenu />
            </CardContent>
          </CardActionArea>
        </Card>
     </div>
  );
};

Now you have a component called NewsCard that taks props of type

{ title: string, newsContent: string | any }

Now you have a list of

{
    title: 'Lorem ipsum',
    newsContent: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis hendrerit dui ac accumsan consequat.'
}

Lets say that you using App component to show the list of cards

function App(){

return (
    <>
    {newsList.map((cardListItem, index)=> <NewsCard key={`card-${index}`} title={cardListItem.title} newsContent={cardListItem.newsContent} /> )}
    </>
    );
}

assuming that NewsCard is in the same file otherwise you need to import it.

For more info please visit the Docs

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.