2

In a React form with several fields (name, email, password, etc.), is there any substantial difference between:

Using a separate useState for each field

const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");

Using a single useState with an object (e.g., typed with an interface) holding all fields

interface RegisterFormData {
  firstName: string;
  lastName: string;
  email: string;
  password: string;
}

const [formData, setFormData] = useState<RegisterFormData>({
  firstName: "",
  lastName: "",
  email: "",
  password: "",
});

I’m interested in differences in state management, performance, and best practices for medium-to-complex controlled forms.

8
  • 1
    These React things are very specific. Code would not help anyone unfamiliar with React. Commented Jan 19 at 22:02
  • 1
    You can include code here exactly like StackOverflow. Commented Jan 19 at 23:13
  • 2
    Adding code to demonstrate a certain conceptional question is perfectly fine on this site. I would also recommend to have in mind not every reader here knows React and what a useState in React might be, still some of those people may be able to answer your question when you take the time to give a short explanation on a conceptional level what a useState is good for or how it works. Commented Jan 20 at 6:42
  • 1
    @DocBrown while this occasionally might be true in general, state management in React is VERY specific. It makes no sense to follow any advice given based on just a couple of pages of the question. To reinforce the point, I've casually read about useState for years, but I do not use React and am unable to answer. Commented Jan 20 at 7:49
  • 1
    This is the kind of question which shows why most askers prefer LLMs over Stackexchange today. When one posts this at ChatGPT or Deepseek (I tried both), one gets an extensive explanation, with different points of view taken into account, which looks quite plausible to me. When asking here, one gets downvotes. Commented Jan 20 at 13:02

1 Answer 1

1

In the example you gave, fields, in general, do not depend on each other. When I enter my email address, it implies nothing in terms of the password. Therefore, go for separate useState. If you don't, you'll need more code just to handle the updates, as you'll need to combine the value that was actually changed with all the values that didn't.

On the other hand, if there is a relation between the fields, a single useState between them would make sense. For example, entering an email address could have an effect on other fields: you may imagine an application that let you enter a password when you type a “public” email address, but as soon as you type one that belongs to your company, the password disappears and the user sees a field to enter a 2FA PIN code. In this case, having a single useState could make sense.

best practices for medium-to-complex controlled forms

Use Redux.

1
  • This was my thought too. It seems unlikely there will be a performance difference (although tricky to test) but with the object you run the danger of assuming formData.firstnane = "x" will update the property Commented Jan 20 at 15:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.