Two Counters چالش 4 از جلسهی 32، متوسط
هوک useCounter(step) را بنویس. باید یک شیء با سه چیز برگرداند:
- count: عدد شمارنده که از 0 شروع میشود
- add: تابعی که step تا به عدد اضافه میکند
- reset: تابعی که عدد را 0 میکندApp آماده است: شمارندهی گلها یکییکی و شمارندهی امتیاز دهتادهتا جلو میرود.
<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 } from "react";
import { createRoot } from "react-dom/client";
function useCounter(step) {
// your code here
return { count: 0, add: () => {}, reset: () => {} };
}
function App() {
const goals = useCounter(1);
const points = useCounter(10);
return (
<div>
<p id="goals">Goals: {goals.count}</p>
<button id="goal" onClick={goals.add}>Goal!</button>
<p id="points">Points: {points.count}</p>
<button id="ten" onClick={points.add}>+10</button>
<button id="reset" onClick={points.reset}>Reset points</button>
</div>
);
}
createRoot(document.getElementById("root")).render(<App />);
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 32: هوک خودت · چالش بعدی: A Hook with a Timer
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js