Interactive Counter
What you'll build
A React app with a number display and three buttons (+, −, Reset). Clicking a button updates the count using React state, with a color change when the count is negative.
Goals
- Use useState to manage counter state
- Attach event handlers to buttons with onClick
- Apply conditional rendering for negative number styling
- Understand the React state update pattern
Prerequisites
- Basic React component syntax
- Understanding of JSX
- Familiarity with JavaScript functions
Steps
- 1
Set up the component and state
Create a Counter component and declare a
countstate variable initialized to 0 using useState.Codeimport { useState } from "react"; function Counter() { const [count, setCount] = useState(0); return <div>{count}</div>; }Expected result
The number 0 is displayed on the page.
- 2
Add the increment and decrement buttons
Add two buttons that call setCount to increment and decrement the count. Use the updater function form for safety.
Code<button onClick={() => setCount(c => c + 1)}>+</button> <button onClick={() => setCount(c => c - 1)}>−</button>Expected result
Clicking + increases the number; clicking − decreases it.
- 3
Add the reset button
Add a reset button that sets the count back to 0.
Code<button onClick={() => setCount(0)}>Reset</button>Expected result
Reset returns the counter to 0 regardless of the current value.
- 4
Add conditional styling for negative numbers
Use a ternary operator to apply a red color class when the count is negative.
Code<div style={{ color: count < 0 ? "red" : "black" }}>{count}</div>Expected result
The number turns red when negative, black when zero or positive.
Hint
You can use inline styles or conditional className.
- 5
Add state management & event handling
Use useState to manage component state and add onClick/onChange handlers to interactive elements. State is the heart of React components, and event handling lets them respond to user actions.
Codeimport { useState } from "react"; export function MyComponent() { const [count, setCount] = useState(0); const [text, setText] = useState(""); return ( <div> <button onClick={() => setCount(c => c + 1)}> Count: {count} </button> <input value={text} onChange={(e) => setText(e.target.value)} /> </div> ); }Expected result
Clicking the button increments the count; the input content updates in real time as you type.
Try it yourself
import { useState } from "react";
function Counter() {
// TODO: add state and buttons
return <div>Counter</div>;
}
export default Counter;import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<div style={{ color: count < 0 ? "red" : "black", fontSize: "2rem" }}>
{count}
</div>
<button onClick={() => setCount(c => c + 1)}>+</button>
<button onClick={() => setCount(c => c - 1)}>−</button>
<button onClick={() => setCount(0)}>Reset</button>
</div>
);
}
export default Counter;What you learned
You learned the fundamental React state pattern: declare state with useState, update it with event handlers, and use conditional rendering for dynamic styling. This is the building block of all interactive React apps.
Related Resources
Related Lessons
- State
useState fundamentals used in this project.
- Event Handling
onClick handlers for buttons.
Related Practice
- Predict State After Updates
Test state update logic.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.