Can I make dynamic styles in React Native?

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

Say I have a component with a render like this:


Where jewelStyle =

  {
    bo         


        
15条回答
  •  孤城傲影
    2020-12-12 15:56

    I know this is extremely late, but for anyone still wondering here's an easy solution.

    You could just make an array for the styles :

    this.state ={
       color: "#fff"
    }
    
    style={[
      styles.jewelstyle, {
      backgroundColor: this.state.BGcolor
    }
    

    The second will override any original background color as stated in the stylesheet. Then have a function that changes the color:

    generateNewColor(){
      var randomColor = '#'+Math.floor(Math.random()*16777215).toString(16);
      this.setState({BGcolor: randomColor})
    }
    

    This will generate a random hex color. Then just call that function whenever and bam, new background color.

提交回复
热议问题