Skip to content

Instantly share code, notes, and snippets.

@ThiaguinhoLS
Created April 17, 2019 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThiaguinhoLS/548dc874c9e0349304150f10951a6c4d to your computer and use it in GitHub Desktop.
Save ThiaguinhoLS/548dc874c9e0349304150f10951a6c4d to your computer and use it in GitHub Desktop.
REACT - HANDLE EVENTS
<div id="root"></div>
function ActionLink(props) {
function handleClick(e) {
e.preventDefault();
console.log("Link clicked");
}
return (
<a href="#" onClick={ handleClick }>
Click here!
</a>
);
}
class Toggle extends React.Component {
constructor(props) {
console.log("call");
super(props);
this.state = { isToggleOn: true };
this.handleClick = this.handleClick.bind(this);
}
handleClick(e) {
this.setState(state => ({
isToggleOn: !state.isToggleOn
}));
}
render() {
return (
<button onClick={ this.handleClick }>
{ this.state.isToggleOn ? "ON" : "OFF" }
</button>
);
}
}
ReactDOM.render(
<Toggle />,
document.getElementById("root");
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment