B. An input box with no text and an empty paragraph
C. An input box and a paragraph showing 'You typed: hello'
D. A syntax error preventing rendering
Solution
Step 1: Understand controlled input behavior
The input's value is controlled by the state variable text. Typing updates text via setText.
Step 2: Check rendered output
The paragraph displays the current text value, so typing 'hello' shows 'You typed: hello'.
Final Answer:
An input box and a paragraph showing 'You typed: hello' -> Option C
Quick Check:
Controlled input shows typed text [OK]
Hint: Controlled input updates state and shows typed text [OK]
Common Mistakes:
Thinking input is uncontrolled and won't update
Expecting paragraph to be empty
Assuming syntax error without cause
4. Identify the error in this React input handler code:
function MyInput() {
const [value, setValue] = useState('');
function handleChange() {
setValue(e.target.value);
}
return <input value={value} onChange={handleChange} />;
}
medium
A. The event parameter e is missing in handleChange
B. The value state is not initialized
C. The onChange should be onInput
D. The input should not have a value prop
Solution
Step 1: Check the handleChange function signature
handleChange uses e.target.value but does not receive e as a parameter, causing a ReferenceError.
Step 2: Correct the function to accept event
Adding e as a parameter fixes the error: function handleChange(e) { setValue(e.target.value); }.
Final Answer:
The event parameter e is missing in handleChange -> Option A
Quick Check:
Event parameter needed in handler [OK]
Hint: Always pass event parameter to onChange handler [OK]
Common Mistakes:
Forgetting to add event parameter e
Using wrong event like onInput
Removing value prop from controlled input
5. You want to create a form with two input fields, firstName and lastName, and update their values independently using a single useState object. Which code snippet correctly updates the state when either input changes?
A. Updates state but overwrites other fields, losing data
B. Wraps updated field inside another object, breaking state
C. Incorrectly nests state causing wrong structure
D. Correctly merges new field value with existing state
Solution
Step 1: Understand state update with objects
When updating part of an object state, you must merge the new field with existing fields to avoid losing data.
Step 2: Analyze each option
Updates state but overwrites other fields, losing data replaces the entire state with only one field, losing the other. Correctly merges new field value with existing state uses a function to merge previous state with the new field correctly. Options C and D incorrectly nest or wrap the state, breaking the structure.
Final Answer:
Correctly merges new field value with existing state -> Option D
Quick Check:
Use spread operator to merge state updates [OK]
Hint: Use spread ...prev to merge state updates in objects [OK]
Common Mistakes:
Overwriting entire state object losing other fields
Nesting state inside another object accidentally
Not using functional update form for safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.