Next.js starter your AI actually understands. Ship internal tools in days not weeks. Pre-order $199 $499 → [Get it now]

How to handle multiple form fields in React

Managing multiple form fields efficiently is crucial for building scalable React forms without repetitive code. As the creator of CoreUI, a widely used open-source UI library, I’ve built countless form components over 25 years of development experience. From my expertise, the most efficient approach is to use a single state object combined with computed property names to handle all fields dynamically. This pattern reduces code duplication and makes form management much more maintainable.

Use a single state object with computed property names to handle multiple form fields.

const [formData, setFormData] = useState({ name: '', email: '', message: '' })

const handleChange = (e) => {
  setFormData(prev => ({ ...prev, [e.target.name]: e.target.value }))
}

Here the formData state holds all form field values in one object. The handleChange function uses computed property names [e.target.name] to dynamically update the correct field based on the input’s name attribute. The spread operator preserves existing values while updating only the changed field, ensuring efficient re-renders.

Best Practice Note:

This is the same pattern we use in CoreUI form components for scalable form management. Always ensure your input elements have matching name attributes, and consider using this approach with form validation libraries like Formik or React Hook Form for complex forms.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Clone an Object in JavaScript
How to Clone an Object in JavaScript

How to Open All Links in New Tab Using JavaScript
How to Open All Links in New Tab Using JavaScript

Understanding the Difference Between NPX and NPM
Understanding the Difference Between NPX and NPM

How to change opacity on hover in CSS
How to change opacity on hover in CSS

Answers by CoreUI Core Team