繁体   English   中英

简化了条件渲染

[英]React conditional rendering simplified

我将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>
    )
  }

如我所见,您的条件组件几乎相同,唯一的区别是您显示/隐藏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
}

总的来说,我所做的是使用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,
  },
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM