I'm trying to achieve something like,
whenever the user change any input field value, update the state value normally and do something, like submit the form with updated state value.
I'm currently doing
const [data, setData] = useState({
search: '',
field: '',
direction: '',
})
const onHandleChange = (e) => {
setData({
...data,
[e.target.name]: e.target.value,
})
// Do soemthing with the updated data, for now I'll just console.log
console.log(data);
}
<input type="search" name="search" onChange={onHandleChange} value={data.search} />
As on the screenshot, if I type 123
the most recent console log would be 12
It's always 1 step behind :/
One thing I thoght of using a useEffect()
but useEffect
also runs the first render, which I dont want. I wouldn't want the form gets submitted on first render, but only when the user changes anything.