【问题标题】:React Native - Select multiple stylesReact Native - 选择多种样式
【发布时间】:2019-08-29 16:58:48
【问题描述】:

使用 React.js,我们可以使用如下类在多种样式中进行选择:

           <div
              className={
                `r-n-cal-day 
                ${i % 7 == 6 ? classes.weekend : ''}
                ${classes.day} ${bsMonth !== this.props.viewBsMonth ? classes.dayMuted : ''} 
                ${this.isSameDate(adDate) ? classes.today : ''} 
                ${this.isSameDate(adDate, this.state.selectedDate) ? classes.selectedDay : ''} 
                `
              }
              key={`${bsDate} ${bsMonth}`}
              onClick={() => this.onDaySelect(adDate)}>
              {calFns.toDevanagariDigits(bsDate)}
            </div>

我试图在 React-Native 中实现这种选择样式的方式。

我可以使用三元运算符在两种样式中进行选择,如下所示:

        <Text style = { 
          index % 7 == 6 ? styles.weekend : styles.day_text
          }>
            {day.bsDate}
        </Text>

但是要从几种样式中选择一种,我们应该怎么做呢?

if 内部的语句不适用于 JSX。有人可以提出一些建议吗?

提前致谢。

【问题讨论】:

    标签: android ios reactjs react-native styles


    【解决方案1】:

    您可以将 Array 传递给您的样式,例如:

      <View style={[styles.powerblue,{marginLeft:10}]} />
    

    风格多样:

    const styles  = StyleSheet.create({
      powerblue:{width: 50, height: 50, backgroundColor: 'powderblue'},
      rightStyled:{marginRight:10},
    });
    

    这里它将应用样式和内联样式中的样式,您可以传递其他样式表对象,例如:

     <View style={[styles.powerblue,{marginLeft:10},styles.rightStyled]} />
    

    但是大多数属性会覆盖左边的属性。

    例如如果 styles.powerblue 的属性 marginRight 为 0,并且 styles.rightStyled 的属性 marginRight 为 10。然后 marginRight 10 将被应用,因为它位于最右侧

    【讨论】:

      【解决方案2】:

      在 javascript 中执行 if 语句,将结果存储在局部变量中,然后在 JSX 中使用该变量。

      【讨论】:

        【解决方案3】:

        我认为你可以这样做

         <View style={this.some(1)}></View>
        

        对于样式(你可以根据需要自定义)

         some(s) {
                if(s===1)
                return {
                    flex:1,
                  borderRadius: 12,
                 backgroundColor:'red',
                }
                else
                return {
                    flex:1,
                  borderRadius: 12,
                 backgroundColor:'white',
                }
              }
        

        【讨论】:

          猜你喜欢
          • 2017-02-27
          • 1970-01-01
          • 1970-01-01
          • 2016-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-19
          • 1970-01-01
          相关资源
          最近更新 更多