Answers for "how to setstate in class component"

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

react lifecycle hooks

class ActivityItem extends React.Component {
  render() {
    const { activity } = this.props;

    return (
      <div className='item'>
        <div className={'avatar'}>
          <img
            alt='avatar'
            src={activity.actor.avatar_url} />
        </div>

        <span className={'time'}>
          {moment(activity.created_at).fromNow()}
        </span>
        
        <p>{activity.actor.display_login} {activity.payload.action}</p>
        <div className={'right'}>
          {activity.repo.name}
        </div>
      </div>
    )
  }
}
Posted by: Guest on October-11-2020
-1

reactjs update state example

You have to use setState() for updating state in React
{
  hasBeenClicked: false,
  currentTheme: 'blue',
}

setState({
	hasBeenClicked: true
});
Posted by: Guest on November-22-2020

Code answers related to "how to setstate in class component"

Browse Popular Code Answers by Language