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

جواب «The Clear Button» چالش 2 از جلسه‌ی 17

سؤال

کادر #word به state وصل است، ولی دکمه‌ی #clear هیچ کاری نمی‌کند.
به دکمه یک onClick بده که state را خالی ("") کند. کادر و خط #show باید با هم خالی شوند.

راهنما

<button id="clear" onClick={() => setWord("")}>Clear</button>

یک جواب درست

<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 App() {
  const [word, setWord] = useState("");
  return (
    <div>
      <input id="word" value={word} onChange={(event) => setWord(event.target.value)} />
      <button id="clear" onClick={() => setWord("")}>Clear</button>
      <p id="show">{word}</p>
    </div>
  );
}

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

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن