Can I make dynamic styles in React Native?

后端 未结 15 2240
别跟我提以往
别跟我提以往 2020-12-12 15:24

Say I have a component with a render like this:


Where jewelStyle =

  {
    bo         


        
15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 15:55

    If you still want to take advantage of StyleSheet.create and also have dynamic styles, try this out:

    const Circle = ({initial}) => {
    
    
    const initial = user.pending ? user.email[0] : user.firstName[0];
    
        const colorStyles = {
            backgroundColor: randomColor()
        };
    
        return (
            
                {initial.toUpperCase()}
            
        );
    };
    
    const styles = StyleSheet.create({
        circle: {
            height: 40,
            width: 40,
            borderRadius: 30,
            overflow: 'hidden'
        },
        text: {
            fontSize: 12,
            lineHeight: 40,
            color: '#fff',
            textAlign: 'center'
        }
    });
    

    Notice how the style property of the View is set as an array that combines your stylesheet with your dynamic styles.

提交回复
热议问题