جواب «Quiet Counting» چالش 2 از جلسهی 31
سؤال
دکمهی Tap باید ضربهها را بیصدا بشمارد: عدد در جعبهی taps زیاد شود ولی صفحه عوض نشود. فقط وقتی Show زده شد، خط #shown عدد تازه را نشان بدهد.
تابع tap خالی است. کاری کن که هر بار یکی به taps.current اضافه کند.
راهنما
taps.current = taps.current + 1;
یک جواب درست
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
</style>
<div id="root" dir="ltr"></div>
<script type="text/jsx">
import { useState, useRef } from "react";
import { createRoot } from "react-dom/client";
function App() {
const taps = useRef(0);
const [shown, setShown] = useState(0);
function tap() {
taps.current = taps.current + 1;
}
return (
<div>
<button id="tap" onClick={tap}>Tap</button>
<button id="show" onClick={() => setShown(taps.current)}>Show</button>
<p id="shown">Taps: {shown}</p>
</div>
);
}
createRoot(document.getElementById("root")).render(<App />);
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن