I am trying to render Card
ponent based on the number of items in the items
object but I am getting the error on line 6.
import React from "react";
export class Cards extends React.Component {
render() {
let elements = [];
elements = this.props.items.map(item => {
<Card titles = {item.title} imgsrc = {item.urlToImage}
discr = {item.description} urls = {item.url}
/>
})
return (
{elements}
);
}
}
And this is the Card
ponent :
import React from 'react'
export default ({titles, imgsrc, urls, discr}) => {
return (
<div className="cards">
<div className="card">
<div className="cardHead">
<h4>{titles}</h4>
</div>
<div className="cardBody">
<img src={imgsrc} alt="pst-img" />
</div>
<div className="cardFooter">
<p>{discr}</p>
<a className="buttn" href={urls}>
<button>Read More</button>
</a>
</div>
</div>
</div>
)
}
I am trying to render Card
ponent based on the number of items in the items
object but I am getting the error on line 6.
import React from "react";
export class Cards extends React.Component {
render() {
let elements = [];
elements = this.props.items.map(item => {
<Card titles = {item.title} imgsrc = {item.urlToImage}
discr = {item.description} urls = {item.url}
/>
})
return (
{elements}
);
}
}
And this is the Card
ponent :
import React from 'react'
export default ({titles, imgsrc, urls, discr}) => {
return (
<div className="cards">
<div className="card">
<div className="cardHead">
<h4>{titles}</h4>
</div>
<div className="cardBody">
<img src={imgsrc} alt="pst-img" />
</div>
<div className="cardFooter">
<p>{discr}</p>
<a className="buttn" href={urls}>
<button>Read More</button>
</a>
</div>
</div>
</div>
)
}
Share
Improve this question
asked Jan 7, 2019 at 6:37
Amal RajanAmal Rajan
311 gold badge2 silver badges7 bronze badges
1
-
Your
.map
callback is not returning anything. Try returning the JSX instead. – CertainPerformance Commented Jan 7, 2019 at 6:38
2 Answers
Reset to default 3You are not returning the JSX in the .map
you need to do it like this:
import React from "react";
export class Cards extends React.Component {
render() {
let elements = [];
elements = this.props.items.map(item => {
return <Card titles = {item.title} imgsrc = {item.urlToImage}
discr = {item.description} urls = {item.url}
/>
})
return (
{elements}
);
}
}
Or in a Cleaner Way:
import React from "react";
export class Cards extends React.Component {
render() {
return (
{this.props.items.map(item => {
return <Card titles = {item.title} imgsrc = {item.urlToImage}
discr = {item.description} urls = {item.url}
/>
})}
);
}
}
As the first answer, you need to return the JSX in map()
Or using ( )
instead of {}
inside the map()
like this
import React from "react";
export class Cards extends React.Component {
render() {
return (
{this.props.items.map(item => (
return <Card titles = {item.title} imgsrc = {item.urlToImage}
discr = {item.description} urls = {item.url}
/>
))}
);
}
}