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

جواب «No Blank Page» چالش 4 از جلسه‌ی 39

سؤال

دکمه‌ی Shop به صفحه‌ای می‌رود که هنوز ساخته نشده، و کل اپ سفید می‌شود. یک اپ منتشرشده هیچ‌وقت نباید صفحه‌ی سفید نشان بدهد.
- کامپوننت NotFound را بنویس: یک p با id برابر lost و متن Page not found.
- در App کاری کن که اگر SCREENS[screen] چیزی نداشت، NotFound نشان داده شود.

راهنما

const Screen = SCREENS[screen] || NotFound; علامت || یعنی «اگر اولی چیزی نبود، دومی».

یک جواب درست

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

function NotFound() {
  return <p id="lost">Page not found</p>;
}

const SCREENS = { home: Home, scores: Scores };

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

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

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