๐Ÿ“Š We use cookies & analytics to improve your experience. Learn more
Latest

React & Modern Front-End Development Module 2 ยท Lesson 4 of 15

0% Exit to course

Module 2 ยท State, effects and live data Lesson 4 of 15

useState and controlled forms

Learning objective

State is a value that survives a re-render. A controlled input keeps the value in state and re-renders on every keystroke โ€” more code than an uncontrolled input, and the version you can validate and reset.

Worked example

const [q, setQ] = useState("") with value={q} onChange={(e) => setQ(e.target.value)} is the whole pattern. Removing value makes the input uncontrolled and its text invisible to your validation code.

Your practice task

Build a search box with a clear button and a character counter, then a four-field form that refuses to submit while one field is empty.

Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.

Key takeaway

State is a value that survives a re-render. A controlled input keeps the value in state and re-renders on every keystroke โ€” more code than an uncontrolled input, and the version you can validate and reset.

Create free account or sign in

Almost there

15 lessons left of 15

Finish every lesson to unlock the certificate. Each one ends in a practice task โ€” do them and the final project brief writes itself.

Next recommended course Full-Stack Web Development Bootcamp