使用 React Native 进行条件内联样式

bry*_*yan 1 javascript reactjs react-native

这种事情可能吗?

<Text style={{ opacity: ${ "blue" == "blue" ? 1 : 0 }, textAlign:"right" }}>Retry</Text>
Run Code Online (Sandbox Code Playgroud)

我知道这不是你的做法,因为它不起作用。但想知道是否有一种方法可以有条件地创建样式而不创建对象。

lin*_*new 7

你不需要${ },即:

<Text style={{ opacity: "blue" == "blue" ? 1 : 0, textAlign:"right" }}>Retry</Text>
Run Code Online (Sandbox Code Playgroud)

或者要使不透明度完全可选,您可以执行以下操作:

<Text style={{ opacity: "blue" == "blue" ? 1 : null, textAlign:"right" }}>Retry</Text>
Run Code Online (Sandbox Code Playgroud)