In the FeedbackForm example below, a button click updates the message state to "Thank you for your feedback!". Imagine a student accidentally removes the onClick handler from the button. What will happen when the user clicks the button?
FeedbackForm
onClick
function FeedbackForm() { const [message, setMessage] = useState(""); const handleClick = (e) => { setMessage("Thank you for your feedback!"); }; return ( <div> <button>Submit Feedback</button> <p>{message}</p> </div> ); }
The message will still update to "Thank you for your feedback!"
The message will display "undefined"
Nothing will happen when the button is clicked
The page will crash with an error
The button will disappear from the screen
Examine the uncontrolled form below. A student wants to display the input value in real-time as the user types (e.g., in a <p> tag). Why doesn’t this work as expected with the current code?
<p>
function UncontrolledForm() { const inputRef = useRef(); const handleSubmit = () => { alert(`Input Value: ${inputRef.current.value}`); }; return ( <div> <input id="title" type="text" ref={inputRef} /> <button onClick={handleSubmit}>Submit</button> </div> ); }
The handleSubmit function is overriding the input value
handleSubmit
The onChange event handler is missing
onChange
The useRef hook doesn’t track changes to the input value in real-time
useRef
The input needs a value attribute to display updates
value
Consider this controlled form with validation. If the user types nothing in the title field and clicks away (blurring the input), what will happen?
function Form() { const [formState, setFormState] = useState({ title: "" }); const [errors, setErrors] = useState({ title: "" }); const validate = () => { setErrors({ title: formState.title === "" ? "Title is required" : "" }); }; const updateFormState = (e) => { setFormState({ title: e.currentTarget.value }); }; return ( <div> <input name="title" value={formState.title} onChange={updateFormState} onBlur={validate} /> {errors.title && <div style={{ color: "red" }}>{errors.title}</div>} </div> ); }
An error message "Title is required" appears below the input
The input border turns red
The title state resets to "No title"
The form crashes because onBlur is not typed correctly
onBlur
In the checkbox example below, a student modifies the onChange handler to log the checkbox state. Which of these modifications will correctly log whether the checkbox is checked?
function Form() { const [formState, setFormState] = useState({ agree: false }); return ( <div> <input id="agree" name="agree" checked={formState.agree} type="checkbox" onChange={(e) => setFormState({ agree: e.currentTarget.checked })} /> <label htmlFor="agree">I agree</label> </div> ); }
onChange={(e) => console.log(e.currentTarget.value)}
onChange={(e) => console.log(e.currentTarget.checked)}
onChange={() => console.log(formState.agree)}
onChange={(e) => console.log(formState.agree)}
A student builds a form with a select input as shown below. After selecting "BMW," they notice the displayed value doesn’t update. What’s the issue, and how can they fix it?
function Form() { const [formState, setFormState] = useState({ car: "" }); return ( <div> <select id="car" name="car" value={formState.car}> <option value="">Please Select</option> {["Mazda", "Toyota", "BMW"].map((t) => ( <option key={t} value={t}>{t}</option> ))} </select> <p>Selected car: {formState.car}</p> </div> ); }
The value attribute should be checked instead
checked
The onChange handler is missing from the <select>
<select>
The setFormState function needs to be called manually
setFormState
The name attribute is incorrect
name
This content is only available to registered users.Please register at the the home page.