Member-only story

Stop Controlling EVERY Form In React, Use FormData Instead!

Simplify your code with Browser APIs

Mike Cronin
5 min readApr 10, 2024

https://mostlyfocused.com/pages/articles/stop_controlling_react_forms Check this article out on my site for a better reading experience!

One of the first things people learn in React is how to control a form. But unfortunately they never learn to answer a crucial question: should they? In most cases the answer is no. Let’s learn what we can do instead of controlling our forms and the few cases when we should control.

What is a controlled form?

Just so we’re all clear, a controlled form is really just a form with controlled inputs. And a controlled input is when the value of the input comes from state and there is an onChange handler that continuously updates the state with each change. It looks like this:

export default function App() {
const initForm = { name: '', age: '' };
const [formInfo, setFormInfo] = useState(initForm);

const onChange = (e) => {
const { name, value } = e.target;
setFormInfo((prevState) => ({ ...prevState, [name]: value }));
};

const onSubmit = (e) => {
e.preventDefault();
console.log(formInfo);
setFormInfo(initForm); // reset the form
};

const { name, age } = formInfo;

return (
<form…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Responses (3)

Write a response

That's why is important to learn JavaScript first and try to stick with native as much as one can; which is OK in 80% of the cases, not everybody builds dashboards.
I've seen crazy cases in which devs brought React into Wordpress to deal with a form!!! It already has jQuery!

11

I haven't tried it yet, but I appreciate the info, Mike. Thank you.

1

U can use refs also, no need for controlling inputs there as well