I am a beginner in React. I have a pretty simple code. (There are other questions which seems to be duplicate of this but they ain't. Please read the contents also not just the question heading before marking duplicate.)
import React, { Component } from 'react';
class Likes extends Component {
render() {
var music=["linkin park", "led zaplin", "regina spector", "bruno mars"];
return(
{
music.forEach(function (value, index, array) {
<h1>{value}</h1>
})
}
);
}
}
export default Likes;
Is it correct way or should I use state. I wanted to keep it as simple as possible. But I'm getting this.
What is that I am missing. What else I should know.
PS: I dont have any other separate component or code. Everything is there inside the class itself.
.mapandreturnthe <h1>keysalso. Correct me If i'm wrong.syntaxErrorand not the use offorEach(though you will have to use map due to the fact thatmapreturns an array that react will use to render butforEachitself does not return anything. Try[1,2,3].forEach(a => a)and[1,2,3].map(a => a)in your browser console to see the difference). The syntaxError can be fixed likeconst elements = [] music.forEach(function(value, index, array) { elements.push(<h1>{value}</h1>) }) return elementsWithforEachyou build an array of elements urself butmapdoes this automatically