Answers for "react create a state"

15

state react

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>
          Click me
        </button>
      </div>
    );
  }
}
Posted by: Guest on March-13-2020
1

state in react

class BalanceInquiry extends Component {
  constructor(props) {
    super(props);
    this.state = {
      totalIncome: 0,
    };
  }

  render() {
    return <div>totalIncome {this.state.totalIncome}</div>;
  }
}
Posted by: Guest on February-24-2021
3

react initial state

//initial state
const [state, setState] = useState(0)
//change state
setState(1)
//change state with prev state value
setState((prevState) => {prevState + 2}) // 3
Posted by: Guest on September-09-2021

Browse Popular Code Answers by Language