Articles → REACT.JS → Setstate() Function In React.Js
Setstate() Function In React.Js
What Is The UI Element State?
- A button can be in an enabled or disabled state
- A label contains some value and when the value is changed the state is also changed
- A checkbox is either in a checked or unchecked state
Purpose
Example
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
class Test extends Component {
state = {
Increment: 0
};
constructor() {
super()
this.incrementTime = this.incrementTime.bind(this);
}
incrementTime() {
this.setState({ Increment: this.state.Increment + 1 });
}
render() {
return (
<React.Fragment>
<label>{this.state.Increment}</label>
<br/>
<button onClick={this.incrementTime}>Click Me</button>
</React.Fragment>
);
}
}
ReactDOM.render(<Test/>, document.getElementById('root'));
Output
Click to Enlarge