[TIL/Coin Site Project] 2023/11/30
Dark Mode ์ต์ ๐์๋ก๊ณ ์นจ console ์์ToggleColorMode component renderingtheme executed 1theme executed 2MyApp component rendering๋ชจ๋ ์์ด์ฝ ํด๋ฆญ console ์์toggleCo
Dark Mode ์ต์ ๐
import React, { createContext, useContext, useMemo, useState } from "react";
import IconButton from "@mui/material/IconButton";
import Box from "@mui/material/Box";
import { useTheme, ThemeProvider, createTheme } from "@mui/material/styles";
import Brightness4Icon from "@mui/icons-material/Brightness4";
import Brightness7Icon from "@mui/icons-material/Brightness7";
const ColorModeContext = createContext({ toggleColorMode: () => {} });
function MyApp() {
console.log("MyApp component rendering");
const theme = useTheme();
const colorMode = useContext(ColorModeContext);
return (
<Box
sx={{
display: "flex",
width: "100%",
height: "100vh",
alignItems: "center",
justifyContent: "center",
bgcolor: "background.default",
color: "text.primary",
borderRadius: 1,
p: 3,
}}
>
{theme.palette.mode} mode
<IconButton
sx={{ ml: 1 }}
onClick={colorMode.toggleColorMode}
color="inherit"
>
{theme.palette.mode === "dark" ? (
<Brightness7Icon />
) : (
<Brightness4Icon />
)}
</IconButton>
</Box>
);
}
const ToggleColorMode = () => {
console.log("ToggleColorMode component rendering");
const [mode, setMode] = useState("light");
const colorMode = useMemo(
() => ({
toggleColorMode: () => {
console.log("toggleColorMode executed");
setMode((prevMode) => (prevMode === "light" ? "dark" : "light"));
},
}),
[]
);
console.log("theme executed 1");
const theme = useMemo(
() =>
createTheme({
palette: {
mode,
},
}),
[mode]
);
console.log("theme executed 2");
return (
<ColorModeContext.Provider value={colorMode}>
<ThemeProvider theme={theme}>
<MyApp />
</ThemeProvider>
</ColorModeContext.Provider>
);
};
export default ToggleColorMode;
// what is createContext?
// ์ปดํฌ๋ํธ ํธ๋ฆฌ ์์์ ์ ์ญ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํ ์ ์๋๋ก ๊ณ ์๋ ๋ฐฉ๋ฒ?
// ColorModeContext๋ผ๋ ์ปดํฌ๋ํธ ๋ด๋ถ์์๋ toggleColorMode๋ฅผ ์ ์ญ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค๋ ์๋ฏธ์ ์ฝ๋?
// what is useTheme?
// ์๋ ์ฝ๋์์ theme์, mui์์ ๋ค์ํ theme์ ๊ท์ ํด๋์ ํ๋์ ํ
์ด๋ค.
// what is useContext?
// useContext๋ ํจ์ํ ์ปดํฌ๋ํธ ๋ด์์ ์ปจํ
์คํธ์ ํ์ฌ ๊ฐ์ ๋ฐํํ๋ค. ์ด๋ฅผ ํตํด ํด๋น ์ปจํ
์คํธ์ ๊ฐ์ ์ฝ์ ์ ์๋ค.
// what is useMemo?
// ์ปดํฌ๋ํธ ์ฑ๋ฅ ์ต์ ํ์ ์ฌ์ฉ๋๋ react hook
// ๋ ๋๋ง => ์ปดํฌ๋ํธ ํจ์ ํธ์ถ => memoize๋ ํจ์ ์ฌ์ฌ์ฉ(๋์ผํ ๊ณ์ฐ ๋ฐ๋ณตํ ๋ ์ฌ์ฉ)
// what is ThemeProvider?
// ์ ์ญ์ผ๋ก ํ
๋ง๋ฅผ ์ ์ฉ๋ ๋ ์ฌ์ฉ๋๋ ์ปดํฌ๋ํธ
// what is createTheme?
// ํ
๋ง ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ ํจ์
์๋ก๊ณ ์นจ console ์์
1. ToggleColorMode component rendering 2. theme executed 1 3. theme executed 2 4. MyApp component rendering
๋ชจ๋ ์์ด์ฝ ํด๋ฆญ console ์์
1. toggleColorMode executed 2. ToggleColorMode component rendering 3. theme executed 1 4. theme executed 2 5. MyApp component rendering
+) toggleColorMode executed ์ดํ setState()์ ์ํด re-rendering์ด ๋๋ค๋ ์ ์ด '์์ด์ฝ ํด๋ฆญ console'์์ ์ฃผ๋ชฉํ ์ฌํญ์ด๋ค.
+) createContext, useContext, useMemo => ๋ฆฌ์กํธ ๊ณต์๋ฌธ์๋ฅผ ํตํด ์ ๋ฆฌํด์ผ ๋ ํ์์ฑ์ ๋๊ผ๋ค.
+) ๋ณธ ํ๋ก์ ํธ์๋ ์ด๋ป๊ฒ ์ ์ฉํ ๊ฒ์ธ์ง์ ๋ํด ๊ณ ๋ฏผํ ํ์์ฑ์ ๋๊ผ๋ค.
๊ตฌํ ๐


More to read
AI&ML ๊ธฐ์ด
Reference: https://bettermesol.github.io/ml/2019/09/16/ai-ml-dl/AI: ๊ธฐ๊ณ๊ฐ ์ฌ๋์ฒ๋ผ ์๊ฐํ๊ณ ํ๋จํ๊ฒ ๋ง๋๋ ๊ฐ์ฅ ๋์ ๋ฒ์ฃผ์ ๊ธฐ์ ์ ๋๋ค.ML: ๋ฐ์ดํฐ๋ฅผ ํ์ตํ์ฌ ์ค์ค๋ก ๊ท์น์ ์ฐพ์๋ด๋ AI์ ํ ๋ถ์ผ๋ก,
'AI Agent Economy'Novitas : AI Agent๊ฐ ์ง๊ฐ์ ๊ฐ์ง๋ ์ธ์
์ผ๋ง ์ , ๋ฏธ๋์์ ์ฆ๊ถ ๋ฆฌ์์น ๋ฆฌํฌํธ(์ฌํด๋ ์ด๋๋ฆฌ์์ด๋ค: ์์ด์ ํธ ์๋์ Near Automata)๋ฅผ ์ ํ๊ฒ ๋์์ต๋๋ค. AI Agent๋ฅผ ์ธ๊ฐ๊ณผ ํจ๊ปํ ๊ฒฝ์ ์ฃผ์ฒด๋ก ๋ฐ๋ผ๋ณด๋ ์๊ฐ์ ์ ์์ด ์ถฉ๊ฒฉ์ ๋ฐ์๋๋ฌ์ฃ .ํ ๊ฐ์ง ์ง๊ณ ๋์ด๊ฐ ๋ถ๋ถ์ด ์์ต๋๋ค. ์ฐ๋ฆฌ๊ฐ ํํ 'AI'
'ERC-8004'Novitas: AI ์์ด์ ํธ ๊ฒฝ์ ์ฃผ์ฒด
Web 4.0์ ํ ๋ฌธ์ฅ์ผ๋ก ์ ์ํ๋ฉด Sovereign Transact์ ๋๋ค.AI๊ฐ ์ธ๊ฐ์ ํ๋ฝ ์์ด ์ง๊ฐ์ ์์ ํ๊ณ , ๊ฒฐ์ ๋ฅผ ์ํํ๋ฉฐ, ์ธํ๋ผ๋ฅผ ํต์ ํ๋ ์ฃผ๊ถ์ ๊ฒฝ์ ์ฃผ์ฒด๊ฐ ๋๋ ์ธ๊ณ์ ๋๋ค. Web 3.0์ด ๋ธ๋ก์ฒด์ธ ๊ธฐ๋ฐ์ ํ์ค์ํ๋ฅผ ์คํํ๋ค๋ฉด, Web 4.0์ ๊ทธ