بتل کد Battle Codeدارد باز می‌شود…

Finish the Hook چالش 2 از جلسه‌ی 32، آسان

هوک useCounter نیمه‌کاره است: تابع add خالی است و دکمه کاری نمی‌کند.
add را کامل کن تا هر بار یکی به count اضافه کند.

<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() {
  const [count, setCount] = useState(0);
  function add() {
    // your code here
  }
  return { count, add };
}

function App() {
  const { count, add } = useCounter();
  return (
    <div>
      <p id="count">Count: {count}</p>
      <button id="add" onClick={add}>Add</button>
    </div>
  );
}

createRoot(document.getElementById("root")).render(<App />);
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 32: هوک خودت · چالش بعدی: Door and Light

بیشتر بخوان: جاوااسکریپت چیست؟ از دکمه‌ی وب تا Node.js