Hariwarshan
Hariwarshan

Reputation: 313

useState is not updating immediately in react

//state for time
let [time,setTime] = useState("11:15")

//setTime when change occurs in input

let TimeChange = (t) =>{
        console.log('recieved time is:', t)
        setTime(t)
        console.log(time,': is set')
    }

//using TimePicker inside return

        <TimePicker 
           onChange={TimeChange}
           value={time}
           />

what am expecting in console is:

recieved time is 12:00

12:00 is set

what am getting in the console is:

recieved time is: 12:00

11:15 : is set (// initial time)

recieved time is: 12:30 (// this is the next input i gave)

12:00 : is set (// first update)

My question is why its not updating first time itself?

Upvotes: 0

Views: 64

Answers (1)

Anatol Zakrividoroga
Anatol Zakrividoroga

Reputation: 4518

Updating the state using the useState hook does not immediately reflect and update the state but will trigger a re-render.

Upvotes: 1

Related Questions