Can I make dynamic styles in React Native?

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

Say I have a component with a render like this:


Where jewelStyle =

  {
    bo         


        
相关标签:
15条回答
  • 2020-12-12 15:46

    In case someone needs to apply conditions

     selectedMenuUI = function(value) {
           if(value==this.state.selectedMenu){
               return {
                    flexDirection: 'row',
                    alignItems: 'center',
                    paddingHorizontal: 20,
                    paddingVertical: 10,
                    backgroundColor: 'rgba(255,255,255,0.3)', 
                    borderRadius: 5
               }  
           } 
           return {
                flexDirection: 'row',
                alignItems: 'center',
                paddingHorizontal: 20,
                paddingVertical: 10
           }
        }
    
    0 讨论(0)
  • 2020-12-12 15:46

    Actually, you can write your StyleSheet.create object as a key with function value, it works properly but it has a type issue in TypeScript:

    import React from 'react';
    import { View, Text, StyleSheet } from 'react-native';
    
    const SomeComponent = ({ bgColor }) => (
      <View style={styles.wrapper(bgColor)}>
        <Text style={styles.text}>3333</Text>
      </View>
    );
    
    const styles = StyleSheet.create({
      wrapper: color => ({
        flex: 1,
        backgroundColor: color,
      }),
      text: {
        color: 'red',
      },
    });
    
    
    0 讨论(0)
  • 2020-12-12 15:52

    Here is what worked for me:

    render() {
      const { styleValue } = this.props;
      const dynamicStyleUpdatedFromProps = {
        height: styleValue,
        width: styleValue,
        borderRadius: styleValue,
      }
    
      return (
        <View style={{ ...styles.staticStyleCreatedFromStyleSheet, ...dynamicStyleUpdatedFromProps }} />
      );
    }
    

    For some reason, this was the only way that mine would update properly.

    0 讨论(0)
  • 2020-12-12 15:54

    I usually do something along the lines of:

    <View style={this.jewelStyle()} />
    

    ...

    jewelStyle = function(options) {
       return {
         borderRadius: 12,
         background: randomColor(),
       }
     }
    

    Every time View is rendered, a new style object will be instantiated with a random color associated with it. Of course, this means that the colors will change every time the component is re-rendered, which is perhaps not what you want. Instead, you could do something like this:

    var myColor = randomColor()
    <View style={jewelStyle(myColor)} />
    

    ...

    jewelStyle = function(myColor) {
       return {
         borderRadius: 10,
         background: myColor,
       }
     }
    
    0 讨论(0)
  • 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 (
            <View style={[styles.circle, colorStyles]}>
                <Text style={styles.text}>{initial.toUpperCase()}</Text>
            </View>
        );
    };
    
    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.

    0 讨论(0)
  • 2020-12-12 15:55

    You can bind state value directly to style object. Here is an example:

    class Timer extends Component{
     constructor(props){
     super(props);
     this.state = {timer: 0, color: '#FF0000'};
     setInterval(() => {
       this.setState({timer: this.state.timer + 1, color: this.state.timer % 2 == 0 ? '#FF0000' : '#0000FF'});
     }, 1000);
    }
    
    render(){
     return (
       <View>
    
        <Text>Timer:</Text>
        <Text style={{backgroundColor: this.state.color}}>{this.state.timer}</Text>
      </View>
     );
     }
    }
    
    0 讨论(0)
提交回复
热议问题