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

Tune In چالش 1 از جلسه‌ی 33، آسان

App با Provider مقدار dark را پخش می‌کند، ولی Title آن را نمی‌خواند و می‌نویسد Theme: ?.
در Title به‌جای "?" مقدار را با useContext از ThemeContext بگیر.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.dark { background: #1e293b; color: white; }
.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 Title() {
  const theme = "?";
  return <h2 id="title" className={"box " + theme}>Theme: {theme}</h2>;
}

function App() {
  return (
    <ThemeContext.Provider value="dark">
      <Title />
    </ThemeContext.Provider>
  );
}

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

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 33: useContext: حالت روشن و تاریک · چالش بعدی: Turn the Station On

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