Skip to content

Instantly share code, notes, and snippets.

@anthonybrown
Last active November 9, 2017 09:08
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 anthonybrown/67afb53420e3ac40b1c9c41c14e46681 to your computer and use it in GitHub Desktop.
Save anthonybrown/67afb53420e3ac40b1c9c41c14e46681 to your computer and use it in GitHub Desktop.
Using a Class to display our buttons
/* Button class component */
class Button extends React.Component {
handleClick = () => {
this.props.onClickFunction(this.props.incrementValue)
}
render() {
return (
<button
onClick={this.handleClick}>
+{this.props.incrementValue}
</button>
);
}
}
/* Functional Result component */
const Result = (props) => {
return (
<div>{props.counter}</div>
);
};
/* App class component */
class App extends React.Component {
state = { counter: 0 };
incrementCounter = (incrementValue) => {
this.setState((prevState) => ({
counter: prevState.counter + incrementValue
}));
};
render() {
return (
<div>
<Button incrementValue={1} onClickFunction={this.incrementCounter} />
<Button incrementValue={5} onClickFunction={this.incrementCounter} />
<Button incrementValue={10} onClickFunction={this.incrementCounter} />
<Button incrementValue={100} onClickFunction={this.incrementCounter} />
<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