Skip to content

Instantly share code, notes, and snippets.

@etienne-dldc
Created September 10, 2017 09:40
Show Gist options
  • Save etienne-dldc/270e6162ef52f6d973c91c9a0c41c1af to your computer and use it in GitHub Desktop.
Save etienne-dldc/270e6162ef52f6d973c91c9a0c41c1af to your computer and use it in GitHub Desktop.
08 - Counter - React State
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
counter: 0
};
}
increment(amount) {
this.setState(prevState => ({ counter: prevState.counter + amount }));
}
render() {
return (
<div>
<span>{this.state.counter}</span>
{this.state.counter > 0 &&
<button
onClick={() => {
this.increment(-1);
}}
>
-
</button>}
{this.state.counter < 10 &&
<button
onClick={() => {
this.increment(1);
}}
>
+
</button>}
</div>
);
}
}
ReactDOM.render(<Counter />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment