React Native styling with conditional

后端 未结 3 1393
深忆病人
深忆病人 2021-02-01 01:49

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?



        
3条回答
  •  执念已碎
    2021-02-01 02:01

    Use StyleSheet.create to do style composition like this,

    make styles for text, valid text, and invalid text.

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

    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.

提交回复
热议问题