gpt4 book ai didi

react-native - 使用条件 react 原生样式

转载 作者:行者123 更新时间:2023-12-03 09:13:24 25 4
gpt4 key购买 nike

我是 native react 的新手。当出现错误时,我试图更改 TextInput 的样式。

我怎样才能让我的代码不那么难看?

<TextInput
style={touched && invalid?
{height: 40, backgroundColor: 'white', borderRadius: 5, padding: 10, borderWidth: 2, borderColor: 'red'} :
{height: 40, backgroundColor: 'white', borderRadius: 5, padding: 10}}
</TextInput>

最佳答案

使用 StyleSheet.create做这样的风格组合,

为文本、有效文本和无效文本制作样式。

const styles = StyleSheet.create({
text: {
height: 40, backgroundColor: 'white', borderRadius: 5, padding: 10,
},
textvalid: {
borderWidth: 2,
},
textinvalid: {
borderColor: 'red',
},
});

然后将它们与一系列样式组合在一起。
<TextInput
style={[styles.text, touched && invalid ? styles.textinvalid : styles.textvalid]}
</TextInput>

对于数组样式,后一种将合并到前一种,对相同的键进行覆盖规则。

关于react-native - 使用条件 react 原生样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45478621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com