State (short for "Trạng thái") allows managing and updating data that can change within a component.
To use State in a Class Component, we need to declare and initialize the state in the constructor method.
Example
import React from 'react';
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
const Up =()=>{
this.setState({count: this.state.count + 1})
}
return (
<div>
<h2>Số lượng: {this.state.count}</h2>
<button onClick={Up}>Tăng</button>
</div>
);
}
}
export default Counter;
In the example above