<div id="root"></div>
class Lamp extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      switchedOn: false,
      timesSwitched: 0
    };
  }

  switch() {
    this.setState((prevState) => ({
      switchedOn: !prevState.switchedOn
    }));
    this.setState((prevState) => ({
      timesSwitched: prevState.timesSwitched + 1
    }));
  }

  render() {
    return (
      <>
        <p>Lamp is {this.state.switchedOn ? "on" : "off "}</p>
        <p>Switch was used {this.state.timesSwitched} times</p>
        <button onClick={() => this.switch()}>Switch</button>
      </>
    );
  }
}

ReactDOM.render(<Lamp />, document.getElementById("root"));
View Compiled
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js