Add a State property to an Inline Style in React

前端 未结 2 675
逝去的感伤
逝去的感伤 2021-01-03 02:36

I have a react element that has an inline style like this: (Shortened version)

      
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 03:32

    yes its possible check below

    class App extends React.Component {
    
      constructor(props){
        super(props)
        this.state = {
          width:30; //default
        };
      }
    
    
      render(){
    
    //when state changes the width changes
    const style = {
      width: this.state.width
    }
    
      return(
        
    //when button is clicked the style value of width increases
    ); }

    :-)

提交回复
热议问题