Counter With Step Buttons
ReactJS
Easy
7 views
Problem Description
Build a counter with +1, -1, +5, reset buttons.
Output Format
Render a React component.
Constraints
Keep one count state and update it from buttons.
Official Solution
import React, { useState } from 'react';
export default function App() {
const [count, setCount] = useState(0);
return (
<div style={{ padding: 16, width: 360 }}>
<h2 style={{ marginTop: 0 }}>meetcode counter</h2>
<div style={{ fontSize: 28, fontWeight: 800 }}>{count}</div>
<div style={{ display: 'flex', gap: 10, flexWrap: 'wrap', marginTop: 12 }}>
<button type='button' onClick={() => setCount((c) => c - 1)} style={{ padding: '8px 12px', borderRadius: 12, border: '1px solid #ddd', background: '#fff' }}>-1</button>
<button type='button' onClick={() => setCount((c) => c + 1)} style={{ padding: '8px 12px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>+1</button>
<button type='button' onClick={() => setCount((c) => c + 5)} style={{ padding: '8px 12px', borderRadius: 12, border: '1px solid #ddd', background: '#fff' }}>+5</button>
<button type='button' onClick={() => setCount(0)} style={{ padding: '8px 12px', borderRadius: 12, border: '1px solid #ddd', background: '#fff' }}>Reset</button>
</div>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!