I am trying to figure out how to toggle an active class onClick
to change CSS properties.
I have taken many approaches, and read many SO answers. Using
A good sample would help to understand things better:
HTML
CSS
.box {
display: block;
width: 200px;
height: 200px;
background-color: gray;
color: white;
text-align: center;
vertical-align: middle;
cursor: pointer;
}
.box.green {
background-color: green;
}
React code
class App extends React.Component {
constructor(props) {
super(props);
this.state = {addClass: false}
}
toggle() {
this.setState({addClass: !this.state.addClass});
}
render() {
let boxClass = ["box"];
if(this.state.addClass) {
boxClass.push('green');
}
return(
{this.state.addClass ? "Remove a class" : "Add a class (click the box)"}
Read the tutorial here.
);
}
}
ReactDOM.render( , document.getElementById("root"));