A Pen by ThiaguinhoLS on CodePen.
Created
April 17, 2019 03:17
-
-
Save ThiaguinhoLS/548dc874c9e0349304150f10951a6c4d to your computer and use it in GitHub Desktop.
REACT - HANDLE EVENTS
This file contains 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
<div id="root"></div> |
This file contains 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
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"); | |
); |
This file contains 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
<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