Answers for "configuring styled component to support ssr and hydration"

0

configuring styled component to support ssr and hydration

{
  "presets": ["next/babel"],
  "plugins": [["styled-components", { "ssr": true }]]
}
Posted by: Guest on January-14-2021
0

configuring styled component to support ssr and hydration

import Document 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()}
          </>
        ),
      };
    } finally {
      sheet.seal();
    }
  }
}
Posted by: Guest on April-26-2022

Code answers related to "Javascript"

Browse Popular Code Answers by Language