Created
July 31, 2020 10:41
-
-
Save dnshko/b4c3b735792b18d6a9723c35b1affb3e to your computer and use it in GitHub Desktop.
REACT CLASS COMPONENT SYNTAX
This file contains 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 Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
counter: 0, | |
}; | |
this.onIncrement = this.onIncrement.bind(this); | |
this.onDecrement = this.onDecrement.bind(this); | |
} | |
onIncrement() { | |
this.setState(state => ({ counter: state.counter + 1 })); | |
} | |
onDecrement() { | |
this.setState(state => ({ counter: state.counter - 1 })); | |
} | |
render() { | |
return ( | |
<div> | |
<p>{this.state.counter}</p> | |
<button | |
onClick={this.onIncrement} | |
type="button" | |
> | |
Increment | |
</button> | |
<button | |
onClick={this.onDecrement} | |
type="button" | |
> | |
Decrement | |
</button> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment