Skip to content

Instantly share code, notes, and snippets.

@jwilber
Created August 26, 2018 23:02
Show Gist options
  • Save jwilber/8eba64b8baf32aafe20f262665cf3250 to your computer and use it in GitHub Desktop.
Save jwilber/8eba64b8baf32aafe20f262665cf3250 to your computer and use it in GitHub Desktop.
Basic React Counter App
class Button extends React.Component {
handleClick = () => {
this.props.onClickFunction(this.props.incrementValue);
}
render() {
return(
<button
onClick={this.handleClick}>
+{this.props.incrementValue}
</button>
);
}
}
const Result = (props) => {
return (
<div>{props.counter}</div>
);
};
class App extends React.Component {
state = {counter: 0};
incrementCounter = (incrementValue) => {
this.setState((prevState) => ({
counter: prevState.counter + incrementValue
}));
};
render() {
return(
<div>
<Button onClickFunction={this.incrementCounter} incrementValue={5}/>
<Button onClickFunction={this.incrementCounter} incrementValue={15}/>
<Button onClickFunction={this.incrementCounter} incrementValue={25}/>
<Result counter={this.state.counter} />
</div>
);
}
}
ReactDOM.render(<App />, mountNode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment