How can I use functional update with event.currentTarget.value in React?

First, please check my code.

const [name, setName] = useState('nick');

const handleChangeName = (e) => {
  setName(prevState => e.currentTarget.value)
}

return (
  <input value={name} onChange={handleChangeName} />
)

I'm trying to do functional update not

setName(e.currentTarget.value)

However, with this code,

const handleChangeName = (e) => {
  setName(prevState => e.currentTarget.value)
}

I am not getting the right value for some reason. If you know what is the problem or the answer, please let me know! Thank you.

1 answer

  • answered 2022-05-07 06:09 Khalfoun Mohamed El Mehdi

    use target instead of currentTarget

        const handleChangeName = (e) => {
      setName(prevState =>  e.target.value)
    }
    
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum