Skip to content

Instantly share code, notes, and snippets.

@aerrity
Created December 12, 2018 14:10
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 aerrity/c8badfb6e6abdce4dcd75647c80737ac to your computer and use it in GitHub Desktop.
Save aerrity/c8badfb6e6abdce4dcd75647c80737ac to your computer and use it in GitHub Desktop.
React - State and props example
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