Changing style of a button on click

后端 未结 5 809
执念已碎
执念已碎 2020-12-08 10:25

Is it possible to change background-color of my button onClick function?

ex. click background-color: black, another click

5条回答
  •  囚心锁ツ
    2020-12-08 11:07

    You can try to use state to store the color. Maybe this would give you the idea how to solve the problem :

    class Test extends React.Component {
        constructor(){
            super();
    
            this.state = {
               black: true
            }
        }
    
        changeColor(){
           this.setState({black: !this.state.black})
        }
    
        render(){
            let btn_class = this.state.black ? "blackButton" : "whiteButton";
    
            return (
                 
            )
        }
    }
    
    React.render(, document.getElementById('container'));
    

    Here is a fiddle.

提交回复
热议问题