【问题标题】:Object in StyleSheet in React NativeReact Native 中样式表中的对象
【发布时间】:2021-01-08 18:32:03
【问题描述】:

我想知道是否有类似 .less 中可以用 React Native 中的 StyleSheet 对象对一些对象进行分组的事情。

例如,如果我渲染这样的东西:

<View style={styles.wrapper}>
   <View style={styles.wrapper.left}></View>
   <View style={styles.wrapper.right}></View>
</View>

并且有一个像这样的样式表对象:

const styles = StyleSheet.create({
  wrapper: {
    /* Some properties */,
    left: {
      /* Some properties */,
    },
    right: {
      /* Some properties */,
    },
  }
})

谢谢!

【问题讨论】:

    标签: react-native react-native-stylesheet


    【解决方案1】:

    您可以为此目的使用 getter

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

    您可以将其与 StyleSheet.create 一起使用

    let style = {
      wrapper: {
        color: "red",
        position: "absolute",
      },
      
      get left() {
        return {
          ...this.wrapper,
          left: 0,
        };
      },
      
      get right() {
        return {
          ...this.wrapper,
          right: 0,
        };
      },
    };
    
    console.log(style.left)

    let style2 = {
      wrapper: {
        prop: {
          color: "red",
          position: "absolute",
        },
    
        get top() {
          return {
            ...this.prop,
            top: 0,
          };
        },
        get bottom() {
          return {
            ...this.prop,
            bottom: 0,
          };
        },
      },
    };
    
    console.log(style2.wrapper.bottom);

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多