0

I'm trying to update a state by clicking on a th tag such as:

<th value='john' onClick={(e)=>handleClick(e)}>user<th/>

and handleClick() looks like this:

function handleClick(event) {
setUser(event.target.getAtrribute('value'))
console.log(user)
}

Now, I would think that the console would output "john", yet, it doesn't. It outputs whatever user was previously set to. I have to click the th tag twice for it to update to 'john'. Why is it that this happens and is there a solution?

thatsalva
  • 33
  • 1
  • 5
  • In addition to the linked question, i recommend this section in react's documentation: https://react.dev/reference/react/useState#ive-updated-the-state-but-logging-gives-me-the-old-value – Nicholas Tower Aug 21 '23 at 20:14

0 Answers0