Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

NextJS + Material-UI - Warning: Prop className did not match

// The problem is the SSR rendering in Next.js, which produces the style fragment before the page is rendered.

// Using Material UI and Next.js (as the author is using), adding a file called _document.js solved the problem.

// Adjusted _document.js

import React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheets } from '@material-ui/styles'; // works with @material-ui/core/styles, if you prefer to use it.
import theme from '../src/theme'; // Adjust here as well

export default class MyDocument extends Document {
  render() {
    return (
      <Html lang="en">
        <Head>
          {/* Not exactly required, but this is the PWA primary color */}
          <meta name="theme-color" content={theme.palette.primary.main} />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
  // Resolution order
  //
  // On the server:
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. document.getInitialProps
  // 4. app.render
  // 5. page.render
  // 6. document.render
  //
  // On the server with error:
  // 1. document.getInitialProps
  // 2. app.render
  // 3. page.render
  // 4. document.render
  //
  // On the client
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. app.render
  // 4. page.render

  // Render app and page and get the context of the page with collected side effects.
  const sheets = new ServerStyleSheets();
  const originalRenderPage = ctx.renderPage;

  ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
    });

  const initialProps = await Document.getInitialProps(ctx);

  return {
    ...initialProps,
    // Styles fragment is rendered after the app and page rendering finish.
    styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
  };
};
Comment

warning prop classname did not match. server material ui

import React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheets } from '@material-ui/styles'; // works with @material-ui/core/styles, if you prefer to use it.
import theme from '../src/theme'; // Adjust here as well

export default class MyDocument extends Document {
  render() {
    return (
      <Html lang="en">
        <Head>
          {/* Not exactly required, but this is the PWA primary color */}
          <meta name="theme-color" content={theme.palette.primary.main} />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
  // Resolution order
  //
  // On the server:
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. document.getInitialProps
  // 4. app.render
  // 5. page.render
  // 6. document.render
  //
  // On the server with error:
  // 1. document.getInitialProps
  // 2. app.render
  // 3. page.render
  // 4. document.render
  //
  // On the client
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. app.render
  // 4. page.render

  // Render app and page and get the context of the page with collected side effects.
  const sheets = new ServerStyleSheets();
  const originalRenderPage = ctx.renderPage;

  ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
    });

  const initialProps = await Document.getInitialProps(ctx);

  return {
    ...initialProps,
    // Styles fragment is rendered after the app and page rendering finish.
    styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
  };
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: js compare tow object values 
Javascript :: assigned property delete in jquery 
Javascript :: react firebase add doc to collection 
Javascript :: You are trying to create a styled element with an undefined component.You may have forgotten to import it. 
Javascript :: console.table in js without index column 
Javascript :: how to use jquery plugins in angular 8 
Javascript :: setstate react 
Javascript :: discord.js v12 how to set owner commands 
Javascript :: even.target in javascript 
Javascript :: filter in javascript practice exercise 
Javascript :: nested model in angular 
Javascript :: javascript merging arrays 
Javascript :: what is ajax 
Javascript :: mongoose search combine fields 
Javascript :: useref in react 
Javascript :: javascript diffence between a++ and ++a 
Javascript :: google maps load kml file javascript 
Javascript :: nestjs framwork 
Javascript :: react-particles-js not working 
Javascript :: read files in javascript 
Javascript :: side effect, useEffect, return 
Javascript :: Grunt--example gruntfile.js 
Javascript :: flex slider 
Javascript :: detect scroll height 
Javascript :: array of range of numbers 
Javascript :: javascript string length 
Javascript :: native module rnc_asyncsqlitedbstorage tried to override asyncstorage module 
Javascript :: puppeteer block request javascript 
Javascript :: debug javascript in chrome 
Javascript :: currenttarget javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =