I am new to react. While studying it I came across map function. The guy was making a quiz app and used map to iterate over an answers array as:
{this.state.allQuestions[this.state.currentQuestion].answerOptions.map((answerOption) => (
<button onClick={() => this.handleAnswerOptionClick(answerOption.isCorrect)}>{answerOption.answerText}</button>
))}
//allQuestions is an array which contains question as well as an array of its options (answerOptions).
While I am using the same method to iterate over a normal array I do not get the desired result, unless I use return statement. Here is my code:
function Component() {
const arr = [1,2,3,4,5];
return (
<div>
<h1>Component</h1>
{arr.map((item) => {
<button>{item}</button> //does not return anything
//return <button>{item}</button> Gives the output
})}
</div>
);
}
Why is that return is required here but not in the first case?