<! DOCTYPEhtml>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>React Click the button to show div and hide div</title>
    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/[email protected]/babel.min.js"></script>
    <style type="text/css">
      .m-test{width: 100px;height: 100px;background-color: red; }</style>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">
      class Toggle extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            isToggleOn: true.dispaly: 'block'
          };
 
          // This binding is necessary to make 'this' work in the callback
          this.handleClick = this.handleClick.bind(this);
        }
 
        handleClick() {
          this.setState(prevState= > ({
            isToggleOn: !prevState.isToggleOn,
            display: prevState.isToggleOn ? 'none': 'block'
          }));
        }
 
        render() {
          return (
            <div>
              <button onClick={this.handleClick}>
                {this.state.isToggleOn ? 'ON' : 'OFF'}
              </button>
              <div className="m-test" style={{display: this.state.display}} ></div>
            </div>
          );
        }
      }
 
      ReactDOM.render(
        <Toggle />.document.getElementById('root'));</script>
  </body>
</html>
Copy the code