Coding task · JavaScript and React course · Module 3: State and Events

Practice: Controlled Forms

Controlled inputs: value={state} + onChange={(e) => setState(e.target.value)}. React controls the value. onSubmit on the form: e.preventDefault() prevents page reload. State = single source of truth!

Task

Create a login form with the states [username, setUsername] and [password, setPassword]. Both fields must be controlled, e.g. <input value={username} onChange={e => setUsername(e.target.value)} />, and the password field has type="password". The form has onSubmit={handleLogin}, and handleLogin calls e.preventDefault(). After submitting, display the greeting Welcome {username} (store in state that the form has been submitted).