No Blank Page چالش 4 از جلسهی 39، متوسط
دکمهی Shop به صفحهای میرود که هنوز ساخته نشده، و کل اپ سفید میشود. یک اپ منتشرشده هیچوقت نباید صفحهی سفید نشان بدهد.
- کامپوننت NotFound را بنویس: یک p با id برابر lost و متن Page not found.
- در App کاری کن که اگر 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>;
}
const SCREENS = { home: Home, scores: Scores };
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-scores" onClick={() => setScreen("scores")}>Scores</button>
<button id="go-shop" onClick={() => setScreen("shop")}>Shop</button>
<Screen />
</div>
);
}
createRoot(document.getElementById("root")).render(<App />);
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 39: ساختن و منتشر کردن · چالش بعدی: Try Again
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js