[英]Conditionally render border colour React Native
I'm trying to conditionally change my border color based on whether the textInput is under focus or not.我正在尝试根据 textInput 是否在焦点下有条件地更改边框颜色。
<View
style={focus? { ...styles.inputRow, ...bgColorOnFocus }: { ...styles.inputRow, ...bgColorOnBlur }}>
This works as expected, but I'm having to repeat inputRow styles twice.这按预期工作,但我不得不重复 inputRow styles 两次。 I've been trying to improve the code with the following code where I add the object based on whether it's on focus or not and it's throwing an error.
我一直在尝试使用以下代码改进代码,在该代码中,我根据是否在焦点上添加 object 并引发错误。
<View style ={
{...styles.inputRow, focus? ...bgColorOnFocus: ...bgColorOnBlur}}
>
I'm not sure if I'm being silly, or if there's something missing that I'm not familiar with?我不确定我是不是很傻,或者是否缺少一些我不熟悉的东西? Thanks for looking into this!
感谢您查看这个!
Typically a style
prop accept an array as a value:通常,
style
道具接受数组作为值:
<View style={[styles.inputRow, isFocus ? bgColorOnFocus : bgColorOnBlur]}>
is a valid way to conditionally render some style.是有条件地呈现某些样式的有效方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.