๐Ÿšจ Warning: Prop `d` did not match.

๋ฐ•์ƒ์€ยท2022๋…„ 5์›” 10์ผ
0

โœ๏ธ blelog โœ๏ธ

๋ชฉ๋ก ๋ณด๊ธฐ
10/13

๐Ÿค” ๋ฐœ์ƒ ์›์ธ

Next.js๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฆ‰, SSR์„ ํ•˜๋Š” ๊ฒฝ์šฐ์— localstorage์˜ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ svg๋ฅผ ๋ Œ๋”๋งํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ๊ณ ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๐Ÿ™‚ ํ•ด๊ฒฐ๋ฒ•

๊ฒฐ๊ตญ SSR์—์„œ localstorage์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์ด๋ฏ€๋กœ ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ๋ Œ๋”๋ง ๋˜๋„๋ก ๋งŒ๋“ค๋ฉด ๊ฒฝ๊ณ ๊ฐ€ ์—†์–ด์ง‘๋‹ˆ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ์˜ ํ•ต์‹ฌ์€ useEffect()๋ฅผ ์ด์šฉํ•ด์„œ ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ๋ Œ๋”๋ง ๋˜๊ณ  ๋‚˜์„œ ์•„์ด์ฝ˜์„ ๋ Œ๋”๋งํ•˜๋„๋ก ์กฐ๊ฑด์„ ๋„ฃ์–ด์ค€ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

  • ์˜ˆ์‹œ ์ฝ”๋“œ
const { theme, setTheme } = useTheme();

const [loaded, setLoaded] = useState(false);
useEffect(() => {
  setLoaded(true);
}, [setLoaded]);

// jsx๋ถ€๋ถ„... ๋‚˜๋จธ์ง€๋Š” ์ƒ๋žต
<button
  type="button"
  onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
  className="w-10 h-10 rounded-full hover:bg-zinc-400 dark:hover:bg-slate-200 inline-flex justify-center items-center dark:hover:text-black"
>
  {loaded ? (
    <Icon
      icon={theme === "dark" ? ICON.SUN : ICON.MOON}
      $fill
      className="w-7 h-7"
    />
  ) : (
    <Icon icon={ICON.MOON} $fill className="w-7 h-7" />
  )}
</button>

0๊ฐœ์˜ ๋Œ“๊ธ€