Skip to content

Instantly share code, notes, and snippets.

@SastraNababan
Created March 11, 2018 01:14
Show Gist options
  • Select an option

  • Save SastraNababan/94489b13daa9dd61f23e29cb321892c7 to your computer and use it in GitHub Desktop.

Select an option

Save SastraNababan/94489b13daa9dd61f23e29cb321892c7 to your computer and use it in GitHub Desktop.
Counter Component State
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