I want to show the username in the alert box. But so far it's not working. Here is my code:
function App() {
const [nameInForm, setNameInForm] = useState("");
const [username, setUserName] = useState("");
const inputName = (event) => {
setNameInForm(event.target.value);
}
const handleSubmit = () => {
setUserName(nameInForm);
alert("Welcome " + username);
}
return (
<>
<form onSubmit={handleSubmit}>
<input type="text" value={nameInForm} onChange={inputName} />
<button type="submit"> Login </button>
</form>
</>
);
}
If I don't use the form tag and use onClick={handleSubmit} on the button still the code does not work.