Can I make dynamic styles in React Native?

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

Say I have a component with a render like this:


Where jewelStyle =

  {
    bo         


        
15条回答
  •  隐瞒了意图╮
    2020-12-12 16:01

    Yes you can and actually, you should use StyleSheet.create to create your styles.

    import React, { Component } from 'react';
    import {
        StyleSheet,
        Text,
        View
    } from 'react-native';    
    
    class Header extends Component {
        constructor(props){
            super(props);
        }    
    
        render() {
            const { title, style } = this.props;
            const { header, text } = defaultStyle;
            const combineStyles = StyleSheet.flatten([header, style]);    
    
            return (
                
                    
                        { title }
                    
                
            );
        }
    }    
    
    const defaultStyle = StyleSheet.create({
        header: {
            justifyContent: 'center',
            alignItems: 'center',
            backgroundColor: '#fff',
            height: 60,
            paddingTop: 15,
            shadowColor: '#000',
            shadowOffset: { width: 0, height: 3 },
            shadowOpacity: 0.4,
            elevation: 2,
            position: 'relative'
        },
        text: {
            color: '#0d4220',
            fontSize: 16
        }
    });    
    
    export default Header;
    

    And then:

提交回复
热议问题