简体   繁体   English

有条件地渲染边框颜色 React Native

[英]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.

相关问题 如何在本机反应中有条件地呈现警报组件 - How to render Alert Component conditionally in react native 在 React Native 中使用 React Navigation 有条件地渲染 header 右键 - Render header right button conditionally with React Navigation in React Native 使用 Firebase 自定义声明在 React Native 中有条件地呈现 UI - Using Firebase Custom Claims to conditionally render a UI in React Native React Native Navigation:有条件地渲染标签栏徽章不起作用? - React Native Navigation: Conditionally render tab bar badge not working? 有条件地渲染 React-Native FlatList 中的一些项目 - conditionally render some items from React-Native FlatList 当输入被聚焦时,React Native有条件地呈现视图的一部分 - React Native conditionally render part of view while input is focused 如何在 React-Native Flatlist 中有条件地呈现这些项目 - How to conditionally render these items in a React-Native Flatlist React Native:你能根据字符串的存在有条件地渲染 JSX 吗? - React Native: Can you conditionally render JSX based on the existence of a string? 反应:如何有条件地渲染? - React: How to conditionally render? 在 React 中有条件地渲染 class - Render class conditionally in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM