Build a counter with increment, decrement and reset buttons.
Skip to solutionKEEP THE
easyMachine Coding
Build a Counter component
1.0k views
01
Understand the problem
ReactStateEvents
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Solve in
Building a Counter in React
Mental model: A counter is the simplest controlled component — the value lives in the parent and the counter only emits requests to change it. Add min/max/step clamping plus disabled-edge logic and it becomes a genuinely reusable primitive instead of a useState(0) toy.
Solution ready — 2 min read
Classified // press E to declassify
04
React solution
Complete solutionRun Playground
import React, { useState } from 'react';
import Counter from './src/Counter';
export default function App() {
const [count, setCount] = useState(0);
return (
<div style={{ fontFamily: 'sans-serif', padding: 20 }}>
<h1>Counter Demo</h1>
<Counter value={count} onChange={setCount} min={0} max={10} step={1} />
<p style={{ marginTop: 16, fontSize: 18 }}>
Current value: <strong>{count}</strong>
</p>
</div>
);
}05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 4 of 87 decoded in the Frontend Machine Coding track. One more won't hurt.