I want to change the style of buttons when clicked on them.
Below are my files.
App.js:
class Test extends Component {
constructor(){
super();
this.state = {
black: true
}
}
changeColor(){
this.setState({black: !this.state.black})
}
render(){
let btn_class1 = this.state.black ? "blackButton" : "whiteButton";
let btn_class2 = this.state.black ? "whiteButton" : "blackButton";
let btn_class3 = this.state.black ? "whiteButton" : "blackButton";
return (
<div>
<button className={btn_class1}
onClick={this.changeColor.bind(this)}>
Button1
</button>
<button className={btn_class2}
onClick={this.changeColor.bind(this)}>
Button2
</button>
<button className={btn_class3}
onClick={this.changeColor.bind(this)}>
Button3
</button>
</div>
)
}
}
export default App;
color.css:
button{
width: 80px;
height: 40px;
margin: 15px;
}
.blackButton{
background-color: black;
color: white;
}
.whiteButton{
background-color: white;
color: black;
}
At the start I want my button1 to have a black background, and button2 and button3 should have a white background.
When I click on button2, then it should have black background, and button1 and button3 should have white background.
Similarly with button3: when clicked upon, it should have a black background, and button1 and button2 should change to a white background.