Grouping React Form Input Values into One Object State
Instead of writing one useState per input in React, you can group the input values into one object state, and then write the update logic in one function. This helps on screens with many inputs, such as sign-up or contact forms, because you don't need to add a state and a function each time you add a field. However, if you forget to copy the other fields with the spread syntax, the entered values disappear from the state.
The test environment was Ubuntu 24.04, Node.js 22.23.4, React 19.3.1 (development build), TypeScript 7.0.2, esbuild 0.28.2, Playwright 1.63.0, and Chromium 153. I used esbuild for the build. I operated the inputs by running Chromium with Playwright. I did not test production builds, runs with Strict Mode enabled, Firefox, or Safari.
Group the fields into one object and select the field to update by its name attribute
I built a form with five fields (name, email, plan, message, and newsletter opt-in) using one state. The code below is from the test code, with the test-only data-testid attributes removed.
import { useState, type ChangeEvent, type FormEvent } from 'react';
type Profile = {
name: string;
email: string;
plan: 'free' | 'pro';
message: string;
newsletter: boolean;
};
const initialProfile: Profile = {
name: '',
email: '',
plan: 'free',
message: '',
newsletter: false,
};
export function ProfileForm() {
const [profile, setProfile] = useState<Profile>(initialProfile);
const [sent, setSent] = useState<Profile | null>(null);
function handleChange(
e: ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>,
) {
const { name, value } = e.target;
setProfile((prev) => ({ ...prev, [name]: value }));
}
function handleCheck(e: ChangeEvent<HTMLInputElement>) {
const { name, checked } = e.target;
setProfile((prev) => ({ ...prev, [name]: checked }));
}
function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
setSent(profile);
}
return (
<form onSubmit={handleSubmit}>
<label>
名前
<input name="name" value={profile.name} onChange={handleChange} />
</label>
<label>
メール
<input name="email" type="email" value={profile.email} onChange={handleChange} />
</label>
<label>
プラン
<select name="plan" value={profile.plan} onChange={handleChange}>
<option value="free">無料</option>
<option value="pro">有料</option>
</select>
</label>
<label>
お問い合わせ内容
<textarea name="message" value={profile.message} onChange={handleChange} />
</label>
<label>
<input
name="newsletter"
type="checkbox"
checked={profile.newsletter}
onChange={handleCheck}
/>
お知らせメールを受け取る
</label>
<button type="submit">送信</button>
<button type="button" onClick={() => setProfile(initialProfile)}>
リセット
</button>
<pre>{JSON.stringify(profile)}</pre>
<pre>{sent ? JSON.stringify(sent) : ''}</pre>
</form>
);
}
The key part of this approach is { ...prev, [name]: value } in handleChange. ...prev copies all the current values, and [name] takes the input's name attribute value (such as 'email'). If you make each name attribute match a key in the state, the same function handles every field. The official React guide, Updating Objects in State, also has an example that uses [e.target.name] to update several fields with one event handler.
Only the checkbox gets its own handler, handleCheck. A checkbox's state is read from e.target.checked, not e.target.value, and the input receives checked instead of value (<input> reference).
When I filled in the five fields in the test environment, the state became the following object. The object stored in sent on submit was the same.
{"name":"たま","email":"t@example.com","plan":"pro","message":"こんにちは","newsletter":true}
Pressing "Reset" passes initialProfile back in. After pressing it, the state returned to the initial object, the name field was empty, and the checkbox was unchecked. Putting the initial value in a constant outside the component lets both useState and the reset use the same object.
Passing only the changed field erases the other fields
The set function from useState replaces the whole state with the object you pass. It does not merge the object with the previous state. The official guide, Choosing the State Structure, also says that with an object state you cannot update just one field, and you must copy the other fields too. I tested two fields, name and email, by passing an object with only the changed field and leaving out the spread syntax.
function handleChange(e: ChangeEvent<HTMLInputElement>) {
setContact({ [e.target.name]: e.target.value });
}
In TypeScript, this line fails the type check.
bad/BadForm.tsx(10,16): error TS2345: Argument of type '{ [x: string]: string; }' is not assignable to parameter of type 'SetStateAction<Contact>'.
Type '{ [x: string]: string; }' is missing the following properties from type 'Contact': name, email
However, with esbuild as used in the test, the build succeeded despite this type error. When I ran the built code in the browser, the state changed as follows.
| Operation | State |
|---|---|
| (initial render) | {"name":"","email":""} |
| Type "Tama" in the name field | {"name":"たま"} |
| Type "t@example.com" in the email field | {"email":"t@example.com"} |
As soon as I entered an email address, name disappeared from the state. The name field still showed "Tama", so you cannot notice this just by looking at the screen.
The console showed the following two errors. When the value passed to value becomes undefined, React treats the field as changing from a controlled component to an uncontrolled one.
A component is changing a controlled input to be uncontrolled. ...
A component is changing an uncontrolled input to be controlled. ...
When I typed a name, the value of the email field became undefined. When I then typed in the email field, it became a string again. As the <input> reference explains, the value of a controlled input must not be undefined or null.
A typo in a name attribute is not caught by the type check
With the [name] approach, name has the type string. So TypeScript cannot stop a key that does not exist in the state. A form where I mistyped the email field's name attribute as emial passed the type check (the exit code of tsc was 0).
<input name="name" value={contact.name} onChange={handleChange} />
<input name="emial" value={contact.email} onChange={handleChange} />
When I typed "Tama" in the name field and "t@example.com" in the email field one character at a time, the state became the following. The email field stayed empty.
{"name":"たま","email":"","emial":"m"}
The input went into a different key, emial, and email stayed empty. The email field displays contact.email, so whatever I typed, it went back to empty. Only the final "m" remained in emial because each keystroke overwrote it with the value of one character in the empty field.
If you want the type check to catch a mistyped key, route the update through a function that ties the key type to the value type.
type Profile = { name: string; plan: 'free' | 'pro'; newsletter: boolean };
function update<K extends keyof Profile>(key: K, value: Profile[K]) {
setProfile((prev) => ({ ...prev, [key]: value }));
}
// Usage
<input value={profile.name} onChange={(e) => update('name', e.target.value)} />
<select
value={profile.plan}
onChange={(e) => update('plan', e.target.value as Profile['plan'])}
>
{/* options omitted */}
</select>
<input
type="checkbox"
checked={profile.newsletter}
onChange={(e) => update('newsletter', e.target.checked)}
/>
In this form, I typed "Tama" into the name field, selected the Paid plan, and checked the checkbox. The state became {"name":"たま","plan":"pro","newsletter":true}.
K must be one of the keys of Profile, and value must have the type of that key. When I passed a wrong key or value to this update, each case produced a type error.
typecheck/mistakes.tsx(5,8): error TS2345: Argument of type '"nmae"' is not assignable to parameter of type 'keyof Profile'.
typecheck/mistakes.tsx(6,22): error TS2345: Argument of type 'string' is not assignable to parameter of type 'boolean'.
typecheck/mistakes.tsx(7,16): error TS2345: Argument of type '"premium"' is not assignable to parameter of type '"free" | "pro"'.
These are the lines for update('nmae', 'たま'), update('newsletter', 'on'), and update('plan', 'premium'). In the same file, the line that used a string-typed key, setProfile({ ...profile, [key]: 'on' }), put a string into newsletter but did not raise an error.
On the other hand, <select> requires as Profile['plan']. When I removed the as and wrote update('plan', e.target.value), e.target.value is typed as string, which caused the following type error.
typecheck/select-no-as.tsx(12,66): error TS2345: Argument of type 'string' is not assignable to parameter of type '"free" | "pro"'.
The approach that dispatches by the input's name attribute is shorter, and the update approach gets type checking. Choose based on how many fields you have and how much of key mistyping you want the types to prevent.
When updating after a wait, copy the state with an updater function
The code so far passes a function (prev) => ... (an updater function) to setProfile, not an object. The difference from passing an object shows up clearly when the state is updated after a wait, such as looking up an address from a postal code. While you wait, other fields may change.
For the test, I used a function that returns an address after one second instead of a real address lookup.
type Address = { name: string; address: string };
function lookupAddress(): Promise<string> {
return new Promise((resolve) => setTimeout(() => resolve('東京都千代田区'), 1000));
}
// Copy the form as it was when the button was clicked
async function fillStale() {
const address = await lookupAddress();
setForm({ ...form, address });
}
// Copy the state at that moment with an updater function
async function fillUpdater() {
const address = await lookupAddress();
setForm((prev) => ({ ...prev, address }));
}
In the test environment, I pressed the "Fill address" button and, during the one-second wait, typed "Tama" in the name field. With the { ...form, address } form, the name disappeared from both the state and the field as soon as the address arrived. With the updater function form, the name stayed.
| Approach | While waiting | After 1 second |
|---|---|---|
setForm({ ...form, address }) |
{"name":"たま","address":""} |
{"name":"","address":"東京都千代田区"} |
setForm((prev) => ({ ...prev, address })) |
{"name":"たま","address":""} |
{"name":"たま","address":"東京都千代田区"} |

In the GIF, the timing of the address response is set by a signal from the recording script, not by the one-second wait.
The form inside fillStale keeps the value from the render when the button was pressed. As the official guide, "State as a Snapshot", explains, even if an event handler runs asynchronously, the state variable's value in that render does not change. The form at the time of the click had an empty name, so copying it overwrote the name I had just typed. The updater function receives the state at the time React processes the update as prev (useState reference).
When you keep the state as one object, every field update copies the other fields again. If the copy source is stale, the input in the other fields is lost. When in doubt, copy with an updater function, such as setForm((prev) => ({ ...prev, [name]: value })).
If you keep deeply nested objects together, the number of copy levels grows. The official guide, Choosing the State Structure, recommends grouping values that always change together into one state, and avoiding deep nesting. For nested updates, such as changing only the city in an address, you can also use Immer to write shorter update code.





