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

Turn the Station On چالش 2 از جلسه‌ی 33، آسان

هر دو جعبه کانتکست را می‌خوانند، ولی فرستنده‌ای نیست و مقدار پیش‌فرض light را می‌گیرند.
در App دور دو جعبه یک ThemeContext.Provider با value برابر "dark" بگذار تا هر دو تاریک شوند.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.light { background: #f8fafc; color: #0f172a; }
.dark { background: #1e293b; color: white; }
.box { max-width: 260px; margin: 8px auto; padding: 12px; border: 2px solid #64748b; border-radius: 12px; }
</style>

<div id="root" dir="ltr"></div>

<script type="text/jsx">
import { useState, createContext, useContext } from "react";
import { createRoot } from "react-dom/client";

const ThemeContext = createContext("light");

function Box({ id }) {
  const theme = useContext(ThemeContext);
  return <p id={id} className={"box " + theme}>Box {id}: {theme}</p>;
}

function App() {
  return (
    <div>
      <Box id="a" />
      <Box id="b" />
    </div>
  );
}

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

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 33: useContext: حالت روشن و تاریک · چالش بعدی: Light and Dark

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