How to add a CSS class to an element on click - React

前端 未结 4 1937
既然无缘
既然无缘 2021-01-03 01:46

How do you add a CSS class to an existing REACT element on click?

I have a JSFiddle created: https://jsfiddle.net/5r25psub/

In the fiddle, the code only wor

4条回答
  •  [愿得一人]
    2021-01-03 02:26

    You need to add all state parameters to getInitialState, right now the only thing you have is color, so this.state.color is the only thing in there

    When you set your state to className: something, that is the only thing in there now, even color is gone...and that is why the initial color is the normal bland gray

    you have a syntax error in setState as well, its not

    this.setState({className = " green"});
    

    It should be:

    this.setState({className: " green"});
    

    Finally, React.render is deprecated, you need to use ReactDOM.render now

    Fiddle: https://jsfiddle.net/omarjmh/69z2wepo/36597/

提交回复
热议问题