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

Find the Bugs چالش 6 از جلسه‌ی 31، سخت

دکمه‌ی Clear باید سه کار بکند: نشانگر را داخل کادر بگذارد، کادر را خالی کند و خط #n را یکی زیاد کند (Cleared: 1، Cleared: 2، …). ولی برنامه دو اشتباه دارد. هر دو را پیدا و درست کن.
راهنمایی: یکی یک کلمه‌ی جاافتاده است. دیگری عددی است که روی صفحه دیده می‌شود ولی جای اشتباهی نگه داشته شده.

<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 [text, setText] = useState("");
  const boxRef = useRef(null);
  const cleared = useRef(0);

  function clear() {
    boxRef.focus();
    setText("");
    cleared.current = cleared.current + 1;
  }

  return (
    <div>
      <input id="box" ref={boxRef} value={text} onChange={(e) => setText(e.target.value)} />
      <button id="clear" onClick={clear}>Clear</button>
      <p id="n">Cleared: {cleared.current}</p>
    </div>
  );
}

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

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 31: useRef: دست زدن به خود صفحه

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