Upon closer inspection, I've come across an issue that I had not previously noticed. I am unsure if there is a bug within my code or if the onChange function always behaves in this manner, but I am experiencing input delay and am uncertain on how to resolve it. Here is an example to illustrate my point:
https://i.sstatic.net/JOVYU.gif
It is evident that the console only displays the previous state, which is quite inconvenient because it requires the user to click the "Go" button twice in order for it to register the correct state, even if the username and password have been correctly entered.
The code I am working with is relatively simple:
const [state, setState] = useState<LoginStateType>(initialLoginState)
const {username, password} = state
const handleChange = (event: ChangeEventType) => {
const {name, value} = event.target
setState({...state, [name]: value})
console.log(username)
}
{...}
<input type="text" name="username" value={username} onChange={handleChange}/>
<input type="text" name="password" value={password} onChange={handleChange}/>
Any assistance on this matter would be greatly appreciated. Thank you!