Hook
1. Class Component and Function Component
Class Component
class Counter extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0,
};
this.handleIncrease = this.handleIncrease.bind(this);
}
handleIncrease = () => {
this.setState({
counter: this.state.counter + 1,
});
};
render() {
return (
<div>
<p>You clicked {this.state.counter} times</p>
<button onClick={this.handleIncrease}>Click me</button>
</div>
);
}
}Function Component
2. What is Hook?
3. Hook Usage Rules
1. Must only be called at the top level of React functions
2. Must only be used inside React functions
Last updated