'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.
Solution 1:[1]
use target instead of currentTarget
const handleChangeName = (e) => {
setName(prevState => e.target.value)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Khalfoun Mohamed El Mehdi |
