当它只是一个if声明时,我喜欢以下方法:
<div className="skillSection">
{this.state.challengeChoices.length < 0 &&
<ChallengeSkill key={i} {...para2} callback={this.madeSelection} />
}
</div>
当然,if/else 有很多选择:
// Use inline if/else with some more readable spacing/indentation
render() {
return (
<div className="skillSection">
{this.state.challengeChoices.length < 0 ? (
<ChallengeSkill key={i} {...para2} callback={this.madeSelection} />
) : (
<div>False</div>
)}
</div>
)
}
// Define as variable
render() {
let dom = <div>False</div>;
if (this.state.challengeChoices.length < 0) {
dom = <ChallengeSkill key={i} {...para2} callback={this.madeSelection} />;
}
return (
<div className="skillSection">
{dom}
</div>
)
}
// Use another method
getDom() {
if (this.state.challengeChoices.length < 0) {
return <ChallengeSkill key={i} {...para2} callback={this.madeSelection} />;
}
return <div>False</div>;
}
render() {
return (
<div className="skillSection">
{this.getDom()}
</div>
)
}