Created
December 12, 2018 14:10
-
-
Save aerrity/c8badfb6e6abdce4dcd75647c80737ac to your computer and use it in GitHub Desktop.
React - State and props example
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
import React from "react"; | |
import ReactDOM from "react-dom"; | |
class Clicky extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {clickCount: 0}; | |
this.handleClick = this.handleClick.bind(this); | |
} | |
handleClick() { | |
this.setState( prevState => ({ | |
clickCount: prevState.clickCount + 1 | |
})); | |
console.log('clicked'); | |
} | |
render() { | |
return( | |
<div> | |
<h1>{this.state.clickCount} times clicked</h1> | |
<button onClick={this.handleClick}>My name is {this.props.name} click me I love to be clicked</button> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render( | |
<div> | |
<Clicky name="Click Jnr"/> | |
</div>, | |
document.getElementById("root") | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment