A Pen by Etienne Deladonchamps on CodePen.
Created
September 10, 2017 09:40
-
-
Save etienne-dldc/270e6162ef52f6d973c91c9a0c41c1af to your computer and use it in GitHub Desktop.
08 - Counter - React State
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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