Single useState object for multi-input forms
scalable form state and immutable updates.
hold all fields in one state object, use a generic handler keyed by field name, and update immutably by spreading previous state.
WHAT THIS TESTS This checks that you can scale form state without a hook per field and that you understand React's immutable update requirement for objects in state.
A GOOD ANSWER COVERS Instead of a separate useState per input, you keep the entire form in a single object, for example useState with an initial object like name empty, email empty, password empty. You write one generic change handler that accepts a field key and the new value and updates state immutably. The key detail is immutability: you must not mutate the existing object; you create a new object by spreading the previous state and overriding the single changed field, and you should use the functional updater form so concurrent updates use the latest state. A typical handler is setForm(prev spread, then field colon value). Each TextInput then binds value to form for its field and calls onChangeText with a closure that supplies that field name. This scales to many fields, keeps related data together, and makes validation and submission straightforward because the whole payload is one object.
COMMON WRONG ANSWERS Mutating form directly then calling the setter, which can skip re-renders and cause stale UI. Calling the setter with an object containing only the changed field, which discards the other fields. Forgetting the functional updater, risking lost updates when several fields change quickly.
LIKELY FOLLOW-UPS Why must updates be immutable? When would useReducer be cleaner? How do you handle nested form objects?
ONE CONCRETE EXAMPLE State form holds name, email, password. A handler handleChange(field, value) calls setForm(prev returning a spread of prev with field set to value). The email TextInput uses value form.email and onChangeText text calling handleChange('email', text). Typing in one field updates only email while name and password are preserved through the spread.
Interview question
Using a single state object for a form, what is the correct way to update just the email field on change?
- a.Call setForm(prev => spreading prev and overriding email) to preserve other fields immutablyCorrect
- b.Mutate form.email directly then call setForm(form)
- c.Create a separate useState for email after all to avoid spreading
- d.Call setForm with an object containing only the email key
Why? this is the answer
Spreading the previous state and overriding only email keeps the other fields intact while updating immutably. Mutating directly skips proper re-renders, and replacing state with just the email key discards name and password.
Just read this? Test yourself on what you have been reading.
Read the original → react.dev
- #react-native
- #forms
- #usestate
- #immutability
- #state
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react-native — each one lists the topics its interview covers.
See open roles