function App() {
const [name, setName] = useState('');
// *****************
const onNameChange = (e) => {
setName(e.target.value)
console.log(name)
}
const onSubmit = (e) => {
e.preventDefault();
setName('')
}
return (
<div className="App">
<form onSubmit={onSubmit}>
<input type="text" value={name} onChange={onNameChange} />
<button>submit</button>
</form>
</div>
)
}
export default App;
why i'm not able to log complete name in console? for example: if i fill "amit" in the form the log will be like this:
- when type "a" console is blank
- when type "am" console is "a"
- when type "ami" console is "am"
- when type "amit" console is "ami"
why the output is one character less? Is setName is async? if Yes then why async/await doesn't have any effect on it?
NOTE: the same logic in Class Component works fine.