Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want a Button Change Color after is clicked. I am using React js with Material UI tag and want this for my job project that i am currently working ?

What I have tried:

i am using material ui button tag
Posted
Updated 9-Jan-21 22:24pm

1 solution

There could be multiple ways to do it. One being, via use of state to play with style.

Example:
JavaScript
class Test extends React.Component {
    constructor(){
        super();

        this.state = {
           red: true
        }
    }

    changeColor(){
       this.setState({red: !this.state.red})
    }

    render(){
        let btn_class = this.state.red ? "redButton" : "blackButton";

        return (
             <button className={btn_class} onClick={this.changeColor.bind(this)}>
                  Button
             </button>
        )
    }
}

React.render(<Test />, document.getElementById('container'));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900