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

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

این اپ دو صفحه دارد، ولی اصلا بالا نمی‌آید. دو اشتباه دارد؛ هر دو را پیدا و درست کن.
راهنمایی: یکی در خطی است که کامپوننت را از SCREENS برمی‌دارد، و دیگری در onClick یکی از دکمه‌ها.

<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 Home() {
  return <h2 id="home">Home</h2>;
}

function Shop() {
  return <h2 id="shop">Shop</h2>;
}

const SCREENS = { home: Home, shop: Shop };

function App() {
  const [screen, setScreen] = useState("home");
  const Screen = SCREENS.screen;
  return (
    <div>
      <button id="go-home" onClick={() => setScreen("home")}>Home</button>
      <button id="go-shop" onClick={setScreen("shop")}>Shop</button>
      <Screen />
    </div>
  );
}

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

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 36: چند صفحه در یک اپ

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