Skip to content

Instantly share code, notes, and snippets.

@tyrostone
Created April 22, 2017 13:32
Show Gist options
  • Save tyrostone/10b63463a6e35226d7e7f2e34f768130 to your computer and use it in GitHub Desktop.
Save tyrostone/10b63463a6e35226d7e7f2e34f768130 to your computer and use it in GitHub Desktop.
class Button extends React.Component {
render() {
return (
<button onClick={() => this.props.onClickFunction(this.props.incrementValue)}>
+{this.props.incrementValue}
</button>
);
}
};
const Result = (props) => {
return (
<div>{props.counter}</div>
);
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = { counter: 0 };
}
incrementCounter = (incrementValue) => {
this.setState((prevState) => ({
counter: prevState.counter + incrementValue
}));
}
resetCounter = () => {
this.setState(({
counter: 0
}));
}
render() {
return (
<div>
<Button incrementValue={1} onClickFunction={this.incrementCounter} />
<Button incrementValue={5} onClickFunction={this.incrementCounter} />
<Button incrementValue={"Reset"} onClickFunction={this.resetCounter} />
<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