i am trying to make a new component inside a js variable. this is my code
class CourseSales extends Component {
render(){
var Course = this.props.items.map((item,i) => {
return <Course name={item.name} price={item.price} key={i}/>
});
return(
<div>
<h1>Purchase your fav. course</h1>
<p>{this.Course}</p>
</div>
);
}
}
but its not showing result in the course component and console is giving me this error
React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
this.Course
is undefined. Assigning a value to var Course
does not automatically assign that value to this.Course
. Saving the array to Course
is not necessary. I would recommend the following:
class CourseSales extends Component {
render(){
return(
<div>
<h1>Purchase your fav. course</h1>
{this.props.items.map((item,i) => (
<p key={i}>
<Course name={item.name} price={item.price} />
</p>
)}
</div>
);
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments