我目前这样做是为了有条件地呈现某些组件:
render() {
return (
<React.Fragment>
<div id="call_notes_app" className="row">
<NavTree onNavChange={this.changeActiveGroup} />
{this.state.shownGroup == 1 && <DiscoveryGroup/>}
{this.state.shownGroup == 2 && <FinancialGroup/>}
{this.state.shownGroup == 3 && <SalesStuffGroup/>}
</div>
</React.Fragment>
);
}
当我尝试使用switch
语句时,它不起作用(我在控制台中收到 ERROR ABORT):
render() {
return (
<React.Fragment>
<div id="call_notes_app" className="row">
<NavTree onNavChange={this.changeActiveGroup} />
{
switch(this.state.shownGroup) {
case 1:
<DiscoveryGroup/>
break;
case 2:
<FinancialGroup />
break;
default:
<SalesStuffGroup />
}
}
</div>
</React.Fragment>
);
}
有没有办法做到这一点switch
?