C. onChangeText should be a function, not a state variable
D. setEmail should be passed as value
Solution
Step 1: Check onChangeText prop usage
It must be a function to update state, but here it is the state variable email.
Step 2: Understand correct usage
We should pass setEmail to onChangeText to update the state.
Final Answer:
onChangeText should be a function, not a state variable -> Option C
Quick Check:
onChangeText must be a function to update state [OK]
Hint: onChangeText must be a function to update state [OK]
Common Mistakes:
Passing state variable instead of setter function
Removing value prop for controlled input
Misplacing useState call
5. You want to capture user input from two fields, username and password, and show a message only when both are filled. Which approach correctly handles this in React Native?
const [form, setForm] = useState({username: '', password: ''});
// What should onChangeText for username be?
// And how to check if both fields are filled?
hard
A. Use separate states for each field and check username || password
B. Use onChangeText={text => setForm({...form, username: text})} and check form.username && form.password
C. Use onChangeText={setForm} and check form.username === ''
D. Use onChangeText={text => setForm(text)} and check form.length > 0
Solution
Step 1: Update nested state correctly
Use spread operator to keep other fields and update only username: setForm({...form, username: text}).
Step 2: Check both fields are filled
Check if both form.username and form.password are truthy (not empty strings).
Final Answer:
Use onChangeText={text => setForm({...form, username: text})} and check form.username && form.password -> Option B
Quick Check:
Use spread to update form object and check both fields [OK]
Hint: Use spread to update form object and check both fields [OK]
Common Mistakes:
Overwriting whole form state instead of merging
Using OR instead of AND to check fields
Passing wrong function to onChangeText
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.