I'm new to React and struggling with the syntax. I have this block as a div inside my render function. Every change I make goes from one syntax error or another or just doesn't work.
<div className="skillSection">
{
if (this.state.challengeChoices.length < 0) {
this.state.challengeChoices.map((para2, i) =>
<ChallengeSkill key={i} {...para2} callback={this.madeSelection} />)
}
else {
return <div>Hello world</div>
}
}
</div>
Recommend making a function:
renderSkillSection: function(){
if (this.state.challengeChoices.length < 0) {
return this.state.challengeChoices.map((para2, i) =>
<ChallengeSkill key={i} {...para2} callback={this.madeSelection} />)
}
else {
return <div>Hello world</div>
}
},
render: function(){
return (<div className="skillSection">
{this.renderSkillSection()}
</div>)
}
jsx doesn't support conditional statement, but it support ternary operator, so you can do it like this:
<div className="skillSection">
{ this.state.challengeChoices.length < 0 ? (
this.state.challengeChoices.map((para2, i) =>
<ChallengeSkill key={i} {...para2} callback={this.madeSelection} />)) : ( <div>Hello world</div>)
}
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With