• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

Error: "MyDocument.getInitialProps()" --> undefined

21.02.16 20:00 작성 조회수 550

0

Error: "MyDocument.getInitialProps()" should resolve to an object. But found "undefined" instead.

위와 같은 에러가 뜨는데 _document가 로드되기 전에 dom에 접근해서 그런 걸까요? 로그인 페이지에서 로그인하지 않고 회원가입 페이지로 넘어갔다가 뒤로가기를 눌러 다시 뒤로 돌아오는 경우 위와 같은 에러가 발생합니다. 

아래 사진처럼 가장 처음의 initialState가 undefined 되는 이유가 무엇일까요? 이것 때문에 그러는 걸까요?

답변 5

·

답변을 작성해보세요.

0

initialState가 undefined여서 발생하는 문제가 있나요?

0

hyese ch님의 프로필

hyese ch

질문자

2021.02.16

import { useRouter } from 'next/router';
이 부분이 'next/link'로 되어 있었습니다.
router로 바꾸니 제대로 동작하는데 그래도 initialStateundefined 입니다.
initialStateundefined
이것과 initialStateFromGSPorGSSR 의 차이는 무엇인가요?

0

hyese ch님의 프로필

hyese ch

질문자

2021.02.16

혹시 몰라 복붙해봤는데 그래도 안 됩니다

0

hyese ch님의 프로필

hyese ch

질문자

2021.02.16

import Document, { Html, Main, Head, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components';

export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
};
} catch (error) {
console.error(error);
} finally {
sheet.seal();
}
}

render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
넵 같습니다 ㅠㅠ

0

https://github.com/ZeroCho/react-nodebird/blob/master/ch7/front/pages/_document.js

위 링크와 소스가 같나요?