Skip to content

Instantly share code, notes, and snippets.

@mojaray2k
Created May 5, 2020 22:38
Show Gist options
  • Save mojaray2k/bf7703316588237b17708af83c9fd6db to your computer and use it in GitHub Desktop.
Save mojaray2k/bf7703316588237b17708af83c9fd6db to your computer and use it in GitHub Desktop.
Create A Toggle Button in a React Class

Create a Toggle Button in a React Class

import React from 'react';
import ReactDOM from 'react-dom';

class Toggle extends React.Component {
  constructor(props) {
    super(props);
    this.state= {
      on: true
    }
  }

  handleClick = () => {
    // todo
    this.setState({
      on: !this.state.on
    });
  }

  render() {
    const { on } = this.state;
    return (
      <button
        onClick={() => this.handleClick()}
      >{on ? "Off": "On"}</button>
    );
  }
}

ReactDOM.render(
  <Toggle />,
  document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment