Im使用react16和Material-Ui组件。在我的根组件上。我想有条件地在属性上加载选项卡和视图。我设法完成了此功能。但是代码看起来很丑陋,也许可以简化。条件取决于this.props.isSIInstalled
render() {
const {shouldSwipe} = this.props;
return (
<MuiThemeProvider theme={theme}>
<Tabs className=' electric' value={this.state.value}
onChange={this.handleChange.bind(this)} variant="scrollable" id={'tabMenu'}>
<Tab label={1} value={0}/>
<Tab label={2} value={1}/>
<Tab label={3} value={2}/>
{this.props.isSIInstalled
&& <Tab label={4} value={3}/>}
</Tabs>
{
this.props.isSIInstalled ?
<SwipeableViews index={this.state.value} onChangeIndex={this.handleChange.bind(this)} disabled={!shouldSwipe}>
<div>Item One</div>
<div>Item Two</div>
<div>Item Three</div>
<div>Item Four</div>
</SwipeableViews> :
<SwipeableViews index={this.state.value} onChangeIndex={this.handleChange.bind(this)} disabled={!shouldSwipe}>
<div>Item One</div>
<div>Item Two</div>
<div>Item Three</div>
</SwipeableViews>
}
</MuiThemeProvider>
)
}
答案 0 :(得分:0)
正如我所看到的,您的条件组件几乎相同,唯一的区别是您显示/隐藏 ItemFour 取决于this.props.isSIInstalled。因此,您可以将代码简化为:
...
</Tabs>
<SwipeableViews index={this.state.value} onChangeIndex={this.handleChange.bind(this)} disabled={!shouldSwipe}>
<div className="modal-container byod-no-pinch-zoom">
<div>Item One</div>
</div>
<div className="modal-container byod-no-pinch-zoom">
<div>Item Two</div>
</div>
<div className="modal-container byod-no-pinch-zoom">
<div>Item Three</div>
</div>
{
this.props.isSIInstalled && (
<div className="modal-container byod-no-pinch-zoom">
<div>Item Four</div>
</div>
)
}
</SwipeableViews>
此外,如果您希望根据条件渲染绝对不同的视图,则可以执行以下操作:
</Tabs>
{
this.props.isSIInstalled && View1
}
{
!this.props.isSIInstalled && View2
}
答案 1 :(得分:0)
总体而言,我使用的是map函数,该函数是React上的救命稻草,并将一些字符串移动到另一个文件中,使代码更简洁易读。
return (
<MuiThemeProvider theme={theme}>
<div className="noScroll">
<Tabs className='byod-no-pinch-zoom electric' value={this.state.value}
onChange={this.handleChange.bind(this)} variant="scrollable" id={'tabMenu'}>
TAB_INFO.map(x => <Tab label={x.label} id={x.id} />)
{this.props.isSIInstalled
&& <Tab label={getSID('SID_RHMI_BYOD_SOURCE_EXPERIENCES')} value={3} />}
</Tabs>
{
this.props.isSIInstalled ?
<SwipeableViews index={this.state.value} onChangeIndex={this.handleChange.bind(this)} disabled={!shouldSwipe}>
<div className="modal-container byod-no-pinch-zoom">
<JourneyInfo webSocketClient={this.props.webSocketClient} />
</div>
['Item Two','Item Three','Item Four'].map( x=>
<div className="modal-container byod-no-pinch-zoom">
<div>{x}</div>
</div>
)
</SwipeableViews> :
<SwipeableViews index={this.state.value} onChangeIndex={this.handleChange.bind(this)} disabled={!shouldSwipe}>
<div className="modal-container byod-no-pinch-zoom">
<JourneyInfo webSocketClient={this.props.webSocketClient} />
</div>
<div className="modal-container byod-no-pinch-zoom" />
<div className="modal-container byod-no-pinch-zoom" />
</SwipeableViews>
}
</div>
</MuiThemeProvider>
)
}
另一个文件,通常称为strings.js或常量。
const export TAB_INFO = [
{
label: 'SID_RHMI_BYOD_INFO',
value: 0,
},
{
label: 'SID_RHMI_BYOD_MEDIA',
value: 1,
},
{
label: 'SID_RHMI_BYOD_CLIMATE',
value: 2,
},
]