teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

編集

2020/12/03 00:04

投稿

yyyrrr
yyyrrr

スコア17

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- next.js、react, styled-componentsを使って開発をしています。
1
+ react, styled-componentsを使って開発をしています。
2
2
  初期描画時に画面のレイアウトが一瞬崩れてしまいます。
3
3
  多分SSRを使っているのが原因だと思うのですが解決方法がわからないので投稿しました。
4
4
  わかる人いましたらご教授して欲しいです

4

編集

2020/12/03 00:04

投稿

yyyrrr
yyyrrr

スコア17

title CHANGED
@@ -1,1 +1,1 @@
1
- next.js 初期描画時にスタイルが崩れる。
1
+ 初期描画時にスタイルが崩れる。
body CHANGED
File without changes

3

編集

2020/12/01 23:24

投稿

yyyrrr
yyyrrr

スコア17

title CHANGED
File without changes
body CHANGED
@@ -22,11 +22,6 @@
22
22
  const sheet = new ServerStyleSheet();
23
23
  const originalRenderPage = ctx.renderPage;
24
24
 
25
- ctx.renderPage = (): ReturnType<DocumentContext['renderPage']> =>
26
- originalRenderPage({
27
- enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />),
28
- });
29
-
30
25
  const initialProps = await Document.getInitialProps(ctx);
31
26
 
32
27
  return {

2

編集

2020/11/28 21:45

投稿

yyyrrr
yyyrrr

スコア17

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,51 @@
1
1
  next.js、react, styled-componentsを使って開発をしています。
2
2
  初期描画時に画面のレイアウトが一瞬崩れてしまいます。
3
3
  多分SSRを使っているのが原因だと思うのですが解決方法がわからないので投稿しました。
4
- わかる人いましたらご教授して欲しいです
4
+ わかる人いましたらご教授して欲しいです
5
+
6
+
7
+
8
+ // 追記
9
+ ```ここに言語を入力
10
+ import React from 'react';
11
+ import NextDocument, {
12
+ Head,
13
+ Main,
14
+ NextScript,
15
+ DocumentInitialProps,
16
+ DocumentContext,
17
+ } from 'next/document';
18
+ import { ServerStyleSheet } from 'styled-components';
19
+
20
+ class Document extends NextDocument {
21
+ static async getInitialProps(ctx: DocumentContext): Promise<DocumentInitialProps> {
22
+ const sheet = new ServerStyleSheet();
23
+ const originalRenderPage = ctx.renderPage;
24
+
25
+ ctx.renderPage = (): ReturnType<DocumentContext['renderPage']> =>
26
+ originalRenderPage({
27
+ enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />),
28
+ });
29
+
30
+ const initialProps = await Document.getInitialProps(ctx);
31
+
32
+ return {
33
+ ...initialProps,
34
+ styles: [...React.Children.toArray(initialProps.styles), sheet.getStyleElement()],
35
+ };
36
+ }
37
+ render() {
38
+ return (
39
+ <Head>
40
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no" />
41
+ <body>
42
+ <Main />
43
+ <NextScript />
44
+ </body>
45
+ </Head>
46
+ );
47
+ }
48
+ }
49
+
50
+ export default Document;
51
+ ```

1

編集

2020/11/16 01:36

投稿

yyyrrr
yyyrrr

スコア17

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
1
  next.js、react, styled-componentsを使って開発をしています。
2
2
  初期描画時に画面のレイアウトが一瞬崩れてしまいます。
3
3
  多分SSRを使っているのが原因だと思うのですが解決方法がわからないので投稿しました。
4
- わかる人いましたらご教授して欲しいです
4
+ わかる人いましたらご教授して欲しいです