Closed wherehows closed 1 year ago
// 🟢 1번 상황 const Layout = Component => props => { const [isMounted, setIsMounted] = useState(false); useEffect(() => { setIsMounted(true); }, []); if (!isMounted) { return ( <div style={{ visibility: 'hidden', }} > <Component {...props} /> </div> ); } return <Component {...props} />; }; // 🔴 2번 상황 const Layout = Component => props => { if (typeof window === 'undefined') { return ( <div style={{ visibility: 'hidden', }} > <Component {...props} /> </div> ); } return <Component {...props} />; };
https://www.gatsbyjs.com/docs/reference/config-files/gatsby-ssr/#onRenderBody
https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/cache-dir/default-html.js
:root.dark 와 :root .dark :root.dark의 경우, :root의 클래스명이 dark인 경우, :root .dark의 경우, 하위 요소 중 클래스명이 dark인 경우
gatsby-ssr.js에서 document.body.className으로 접근이 불가능한 이유 아래에서 1번 코드는 정상적으로 동작하지만, 2번 코드는 정상적으로 동작하지 않는다. body가 파싱되기 전에 body에 접근하고 있기 때문
// 🟢 1번 코드 if (theme === 'dark') { document.documentElement.className = 'dark'; } else { document.documentElement.className = 'light'; }
// 🔴 2번 코드 if (theme === 'dark') { document.body.className = 'dark'; } else { document.body.className = 'light'; }
The latest updates on your projects. Learn more about Vercel for Git ↗︎
Hydration Matching Error
gatsby-ssr.js 관련
https://www.gatsbyjs.com/docs/reference/config-files/gatsby-ssr/#onRenderBody
https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/cache-dir/default-html.js
다크 모드 관련
:root.dark 와 :root .dark :root.dark의 경우, :root의 클래스명이 dark인 경우, :root .dark의 경우, 하위 요소 중 클래스명이 dark인 경우
gatsby-ssr.js에서 document.body.className으로 접근이 불가능한 이유 아래에서 1번 코드는 정상적으로 동작하지만, 2번 코드는 정상적으로 동작하지 않는다. body가 파싱되기 전에 body에 접근하고 있기 때문
// 🔴 2번 코드 if (theme === 'dark') { document.body.className = 'dark'; } else { document.body.className = 'light'; }