changing class in react to state code example
Example: how to change className by state
class Component extends React.Component {
contructor(props) {
super(props);
this.state = {
isBoxVisible: false;
}
this.toggleBox = this.toggleBox.bind(this);
}
toggleBox() {
this.setState({isBoxVisible: !this.state.isBoxVisible})
}
render() {
const { isBoxVisible } = this.state;
return (
<div className="App">
<button onClick={this.toggleBox}>Show Box</button>
<div className={`box ${isBoxVisible ? "" : "hidden"}`}>
<p>I'm the box</p>
</div>
</div>
);
}
}