Why do you need Controlled Forms?
One reason why someone might use controlled inputs is to validate the input before submission.
The overall process of now controlled inputs work
- user types -> calls handleChange -> sets the data based on the name
NOTE: setName/setUsername/etc are async
An example of how to implement controlled inputs in react hooks
import React, { useState } from "react";
import "./styles.css";
export default function App() {
// Where our state is being stored
const [name, setName] = useState("");
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
// Everytime input changes we update the state
const handleChange = (e) => {
if (e.target.name === "name") {
setName(e.target.value);
} else if (e.target.name === "username") {
setUsername(e.target.value);
} else if (e.target.name === "email") {
setEmail(e.target.value);
} else if (e.target.name === "password") {
setPassword(e.target.value);
}
};
const handleSubmit = (e) => {
e.preventDefault();
console.log("name: ", name)
console.log("username: ",username)
console.log("email: ",email)
console.log("password: ",password)
}
// The value will be based on the state
return (
<div className="App">
<h1>Controlled input</h1>
<form onSubmit={handleSubmit}>
<div>
<label>name</label>
<input
type="text"
name="name"
value={name}
onChange={handleChange}
></input>
</div>
<div>
<label>username</label>
<input
type="text"
name="username"
value={username}
onChange={handleChange}
></input>
</div>
<div>
<label>email</label>
<input
type="email"
name="email"
value={email}
onChange={handleChange}
></input>
</div>
<div>
<label>password</label>
<input
type="password"
name="password"
value={password}
onChange={handleChange}
></input>
</div>
<button type="submit">Submit</button>
</form>
</div>
);
}
Thank You for reading! I know that the part where we update the state can be improved to be more DRY, but I just find that the if-else statements make it easy to understand. If you have a better way to implementing that part let me know in the comments. This is usually what I would use when I implement a controlled form input.
Top comments (5)
We can do better using an object, spread operator and computed properties instead of bunch of ifs.
This approach is more scalable and even allow sending the object as JSON easily.
Yea, my approach wasn't the best. I was justing so used to seeing my state separated like that, that I just kept doing it. Your code is so much better and much more scaleable! Thank You for sharing! I'll be doing this from now on!
Why do you use different state objects for each input?
This part of your code
Can be simplified to this
Your right, my code isn't the best. Thank you for sharing how you would do it! I've near seen someone do this before. Pretty cool
This is a feature of ES6, called destructuring.
So actually it can be used on function parameters as well
There is more to this than the examples above.
Documentation -> developer.mozilla.org/en-US/docs/W...