change color on click react code example
Example 1: how to change list item text color in react
<ListItemText
primary={
<Typography variant="h6" style={{ color: "white" }}>
User
</Typography>
}
secondary={
<Typography style={{ color: "white" }}>hello</Typography>
}
/>
Example 2: change style on click react
Html
<div id="app"></div>
Css
button{
width: 80px;
height: 40px;
margin: 15px;
}
.blackButton{
background-color: black;
color: white;
}
.whiteButton{
background-color: white;
color: black;
}
React
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 (
<div>
<button className={btn_class}
onClick={this.changeColor.bind(this)}>
Button
</button>
</div>
)
}
}
ReactDOM.render(<Test />, document.querySelector("#app"))