setstate in class component react code example

Example 1: state with react functions

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0);

  return (
    

You clicked {count} times

); }

Example 2: this setstate previous state react

state = {count: 0}

increment = () => { 
this.setState(previousState => ({
count: previousState.count + 1
}))
}

Example 3: set state

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

Tags:

Misc Example