Last active
May 3, 2020 23:42
-
-
Save anmolsukki/15475b2c76dbc4bd9d09aeb5afdbd159 to your computer and use it in GitHub Desktop.
[ ReactJs ] create "ON OFF" toggle button (https://codesandbox.io/s/toggle-yzxy2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from "react"; | |
| class App extends React.Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| isToggleOn: true, | |
| isToggleAdd: true | |
| }; | |
| } | |
| handleClickOn = () => { | |
| this.setState(prevState => ({ | |
| isToggleOn: !prevState.isToggleOn | |
| })); | |
| }; | |
| handleClickAdd = () => { | |
| this.setState(prevState => ({ | |
| isToggleAdd: !prevState.isToggleAdd | |
| })); | |
| }; | |
| render() { | |
| return ( | |
| <div> | |
| <button onClick={this.handleClickOn}> | |
| {this.state.isToggleOn ? "ON" : "OFF"} | |
| </button> | |
| <br /> | |
| <br /> | |
| <button onClick={this.handleClickAdd}> | |
| {this.state.isToggleAdd ? "ADD" : "REMOVE"} | |
| </button> | |
| </div> | |
| ); | |
| } | |
| } | |
| export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment