Created
March 11, 2018 01:14
-
-
Save SastraNababan/94489b13daa9dd61f23e29cb321892c7 to your computer and use it in GitHub Desktop.
Counter Component 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{ | |
| // #1 inisialisasi state | |
| state={ | |
| value :0 | |
| } | |
| // #2 method untuk merubah state | |
| minus=()=>{ | |
| let currentValue=this.state.value | |
| this.setState({ value : currentValue - 1 }) | |
| } | |
| // #2 method untuk merubah state | |
| plus=()=>{ | |
| let currentValue=this.state.value | |
| this.setState({ value : currentValue + 1 }) | |
| } | |
| render(){ | |
| // #3 read component state | |
| let currentValue=this.state.value | |
| // #4 call plus method | |
| this.plus() | |
| console.log(currentValue) //=> 1 | |
| // #4 call minus method | |
| this.minus() | |
| console.log(currentValue) //=> -1 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment