Controlled Inputs
Part of the Fundamentals section of Coddy's React journey — lesson 28 of 42.
Text inputs bring a question: the user types into the box, but your component has state: who owns the text? In React the answer is: state owns it. That's called a controlled input:
const [name, setName] = useState('');
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>Two wires make the loop:
value={name}: the box always displays the stateonChange: every keystroke calls the setter withe.target.value, the box's current text
So typing updates state, state re-renders, the box shows the new state. The payoff: the text lives in a variable you can validate, transform, clear or display anywhere else on the page.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2JSX Expressions
Curly BracesMath in BracesBraces in AttributesCalling Functions in JSXRecap: JSX Expressions5State & Events
What Is State?Click CounterCount in StepsToggle with StateIndependent StateRecap: State & Events8Project: Profile Card
Step 1: Card StructureStep 2: Card with PropsStep 3: Like ButtonStep 4: Follow Toggle