Welcome 🎉

logo

ReactLMS

Search
Light Mode
Contact Us

1 min to read

Contact us

No results for your search.
Sorry, an unexpected error occurred

Concept


State (short for "Trạng thái") allows managing and updating data that can change within a component.


Using State in Class 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




Read more
On This Page