In this lesson, you will practice implementing controlled forms and other controlled components within React.
You are working with a startup that wants to create an interface allowing the HR department to add employees to a list, including their roles within the company. To ensure the correct credentials, users must input and validate a password.
- Add a controlled search and a controlled form to the webpage.
└── App
├── Header
├── EmployeeForm
└── EmployeeList
└── Employee
- The
employees
state is stored inApp
, asEmployeeList
needs it for rendering andEmployeeForm
updates it.
- Fork and clone the repository.
- Open the project in VSCode.
- Run the following commands:
npm install npm run dev
Modify EmployeeForm.jsx
:
import React, { useState } from "react";
function EmployeeForm() {
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [role, setRole] = useState("");
const [password, setPassword] = useState("");
return (
<form className="NewItem">
<input type="text" name="firstName" placeholder="First Name" />
<input type="text" name="lastName" placeholder="Last Name"/>
<input type="text" name="role" placeholder="Role Name"/>
<input type="text" name="password" placeholder="Password"/>
<button type="submit">Add Employee</button>
</form>
);
}
export default EmployeeForm;
Modify EmployeeForm.jsx
:
import React, { useState } from "react";
function EmployeeForm() {
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [role, setRole] = useState("");
const [password, setPassword] = useState("");
return (
<form className="NewItem">
<input type="text" name="firstName" placeholder="First Name" onChange={(e) => setFirstName(e.target.value)} />
<input type="text" name="lastName" placeholder="Last Name" onChange={(e) => setLastName(e.target.value)} />
<input type="text" name="role" placeholder="Role Name" onChange={(e) => setRole(e.target.value)} />
<input type="text" name="password" placeholder="Password" onChange={(e) => setPassword(e.target.value)} />
<button type="submit">Add Employee</button>
</form>
);
}
export default EmployeeForm;
Modify EmployeeForm.jsx
:
import React, { useState } from "react";
function EmployeeForm() {
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [role, setRole] = useState("");
const [password, setPassword] = useState("");
function handleSubmit(e) {
e.preventDefault();
if (password === "admin") {
const newUser = { firstName, lastName, role };
console.log("Adding New User:", newUser);
} else {
console.log("Not valid password");
}
}
return (
<form className="NewItem" onSubmit={handleSubmit}>
<input type="text" name="firstName" placeholder="First Name" onChange={(e) => setFirstName(e.target.value)} />
<input type="text" name="lastName" placeholder="Last Name" onChange={(e) => setLastName(e.target.value)} />
<input type="text" name="role" placeholder="Role Name" onChange={(e) => setRole(e.target.value)} />
<input type="text" name="password" placeholder="Password" onChange={(e) => setPassword(e.target.value)} />
<button type="submit">Add Employee</button>
</form>
);
}
export default EmployeeForm;
Modify EmployeeForm.jsx
:
import React, { useState } from "react";
function EmployeeForm({ employees, setEmployees }) {
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [role, setRole] = useState("");
const [password, setPassword] = useState("");
function handleSubmit(e) {
e.preventDefault();
if (password === "admin") {
const newUser = { firstName, lastName, role };
setEmployees([...employees, newUser]);
} else {
console.log("Not valid password");
}
}
return (
<form className="NewItem" onSubmit={handleSubmit}>
<input type="text" name="firstName" placeholder="First Name" onChange={(e) => setFirstName(e.target.value)} />
<input type="text" name="lastName" placeholder="Last Name" onChange={(e) => setLastName(e.target.value)} />
<input type="text" name="role" placeholder="Role Name" onChange={(e) => setRole(e.target.value)} />
<input type="text" name="password" placeholder="Password" onChange={(e) => setPassword(e.target.value)} />
<button type="submit">Add Employee</button>
</form>
);
}
export default EmployeeForm;
Modify App.jsx
:
import { useState } from "react";
import "./App.css";
import EmployeeForm from "./components/EmployeeForm";
import employeeData from "./data/employees";
import Header from "./components/Header";
import EmployeeList from "./components/EmployeeList";
function App() {
const [employees, setEmployees] = useState(employeeData);
return (
<div>
<Header />
<EmployeeForm employees={employees} setEmployees={setEmployees} />
<EmployeeList employees={employees} />
</div>
);
}
export default App;
- Debug and test during coding.
- Add descriptions to event functions to improve organization.
- Controlled forms can use individual states per input or a single object state.