useState
A Hook that lets you add React state to function components.
class Counter extends React.Component {
constructor(props) {
this.state = {
count: 0 // initially the count is set to 0
};
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me // this.setState will increase the count by 1 everytime
</button>
</div>
);
}
}Last updated