state setstate code example

Example 1: set state

class App extends React.Component {

state = { count: 0 }

handleIncrement = () => {
  this.setState({ count: this.state.count + 1 })
}

handleDecrement = () => {
  this.setState({ count: this.state.count - 1 })
}
  render() {
    return (
      <div>
        <div>
          {this.state.count}
        </div>
        <button onClick={this.handleIncrement}>Increment by 1</button>
        <button onClick={this.handleDecrement}>Decrement by 1</button>
      </div>
    )
  }
}

Example 2: setstate in react

Functional Component
const [counter, setCounter] = useState(0);

setCouter(counter + 1);

Example 3: react setState

this.setState({
      date: new Date()
    });

Example 4: set state

setState({ searchTerm: event.target.value })

Example 5: reactjs update state example

{
  hasBeenClicked: false,
  currentTheme: 'bluesss',
}s