Can I make dynamic styles in React Native?

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

Say I have a component with a render like this:


Where jewelStyle =

  {
    bo         


        
15条回答
  •  悲&欢浪女
    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 (
       
    
        Timer:
        {this.state.timer}
      
     );
     }
    }
    

提交回复
热议问题