Echo the Input
Part of the Fundamentals section of Coddy's React journey — lesson 29 of 42.
Once input text lives in state, showing it anywhere is just braces. A greeting that follows the box as you type:
const [name, setName] = useState('');
<input value={name} onChange={(e) => setName(e.target.value)} />
<p>{name === '' ? 'Type your name' : 'Hello, ' + name + '!'}</p>The ternary handles the empty box with a friendly prompt instead of an awkward Hello, !. This input-plus-live-output pattern is search boxes, signup forms and editors everywhere.
Challenge
EasyBuild a live greeting.
- Add
namestate starting as an empty string and make the input (id="name") controlled:value+onChange. - The paragraph (
id="echo") showsType your namewhile the box is empty, andHello, ...!with the typed name otherwise (e.g. typingAda→Hello, Ada!).
Try it yourself
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="root"></div>
<script type="module" src="main.jsx"></script>
</body>
</html>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