I'm new to react native. I'm trying to change the styling of the TextInput when there is an error.
How can I make my code not as ugly?
<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>
Update your code as following:
Then outside your class component, write:
Use
StyleSheet.create
to do style composition like this,make styles for text, valid text, and invalid text.
and then group them together with an array of styles.
For array styles, the latter ones will merge into the former one, with overwrite rule for the same keys.