Skip to content

Instantly share code, notes, and snippets.

@anmolsukki
Last active May 3, 2020 23:42
Show Gist options
  • Select an option

  • Save anmolsukki/15475b2c76dbc4bd9d09aeb5afdbd159 to your computer and use it in GitHub Desktop.

Select an option

Save anmolsukki/15475b2c76dbc4bd9d09aeb5afdbd159 to your computer and use it in GitHub Desktop.
[ ReactJs ] create "ON OFF" toggle button (https://codesandbox.io/s/toggle-yzxy2)
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