首页 > 解决方案 > 带有三元运算符的 CSS - 仅在一种情况下设置属性

问题描述

我在我的 CSS 中使用 react native Platform API 作为三元运算符。

例如,我正在设置高度:

height: Platform.OS === 'ios' ? 40 : 50,

但是,有时我想在一种情况下设置属性,但在另一种情况下却不想:

height: Platform.OS === 'ios' ? 40 : <Don't set height>

实现这一目标的正确逻辑是什么?

标签: javascriptcssreact-native

解决方案


The approach i use is by making 2 style components :

const styles = StyleSheet.create({
  withHeight: {
    color: 'red',
height:40
  },
withoutHeight:{
color:'red'
}
});

So in your component you can do like :

<View style={Platform.OS =='ios'?styles.withHeight:styles.withoutheight}>

Hope it helps. feel free for doubts


推荐阅读