1class Example extends React.Component {
2 constructor(props) {
3 super(props);
4 this.state = {
5 count: 0
6 };
7 }
8
9 render() {
10 return (
11 <div>
12 <p>You clicked {this.state.count} times</p>
13 <button onClick={() => this.setState({ count: this.state.count + 1 })}>
14 Click me
15 </button>
16 </div>
17 );
18 }
19}
1class BalanceInquiry extends Component {
2 constructor(props) {
3 super(props);
4 this.state = {
5 totalIncome: 0,
6 };
7 }
8
9 render() {
10 return <div>totalIncome {this.state.totalIncome}</div>;
11 }
12}
13
1 <button onClick={() => this.setState({ count: this.state.count + 1 })}>
2 Click me
3 </button>
1// State is essentially a global class variable
2// that is modified when the component updates
3
4class Clock extends React.Component {
5 constructor(props) {
6 super(props);
7 this.state = {date: new Date()};
8 }
9
10 render() {
11 return (
12 <div>
13 <h1>Hello, world!</h1>
14 <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
15 </div>
16 );
17 }
18}