Character Counter
Part of the Fundamentals section of Coddy's React journey — lesson 30 of 42.
Because the text is state, you can compute with it on every render. Tweet-style length counters are one line:
<p>{message.length} characters</p>No extra state needed: message.length is derived from existing state, recalculated automatically on every keystroke. Rule of thumb: if you can compute it from state you already have, don't store it in state.
Challenge
EasyBuild a bio box with a live length meter.
- Make the input (
id="bio") controlled withbiostate (start empty). - The paragraph (
id="length") showsN characterswhere N is the bio's current length (computed, not stored).
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