Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

problem with Next.js and @material-ui.

//It's a problem with Next.js and @material-ui. The problem has something to do with SSR (Server Side Rendering) so you have to cancel it and remove the server side files.

//step 1: create _document.js under pages folder

//step 2: add this to the documents file

import { ServerStyleSheets } from '@mui/styles';
import Document, { Head, Html, NextScript, Main } from 'next/document';
import React from 'react';

export default class MyDocument extends Document {
  render() {
    return (
      <Html lang="en">
        <Head></Head>
        <body>
          <Main></Main>
          <NextScript></NextScript>
        </body>
      </Html>
    );
  }
}

MyDocument.getInitialProps = async (ctx) => {
  const sheets = new ServerStyleSheets();
  const originalRenderPage = ctx.renderPage;
  ctx.renderPage = () => {
    return originalRenderPage({
      enchanceApp: (App) => (props) => sheets.collect(<App {...props} />),
    });
  };
  const initialProps = await Document.getInitialProps(ctx);
  return {
    ...initialProps,
    styles: [
      ...React.Children.toArray(initialProps.styles),
      sheets.getStyleElement(),
    ],
  };
};

//step 3: inside the _app.js add this inside the main function component

  useEffect(() => {
    const jssStyles = document.querySelector('#jss-server-side');
    if (jssStyles) jssStyles.parentElement.removeChild(jssStyles);
  }, []);

//restart the Next.js app and you're done
Comment

PREVIOUS NEXT
Code Example
Javascript :: Validation Script Rule 
Javascript :: Watch an API for Updates 
Javascript :: vue append component to div 
Javascript :: How to Loop Through an Array with a do…while Loop in JavaScript 
Javascript :: Backbone View El 
Javascript :: geteliment by id in javascript 
Javascript :: &quot in json 
Javascript :: angular service await for data 
Javascript :: Check If Backbone Model Has Property 
Javascript :: How to redirect to login page if not logged in javascript 
Javascript :: Backbone View Notes 
Javascript :: Solution-1--solution options for reverse bits algorithm js 
Javascript :: remove duplicate node 
Javascript :: hide and show button react js 
Javascript :: parse json 
Javascript :: get class name of object javascript 
Javascript :: why null is object in javascript 
Javascript :: how to nested schema mongoose 
Javascript :: vuejs methods 
Javascript :: timout 
Javascript :: Node.js (node 11.12.0) sample 
Javascript :: strip whitespace from shopify liquid output 
Javascript :: get cos in degree javascript 
Javascript :: JavaScript Constructor Function Parameters 
Javascript :: JavaScript Destructuring - Before ES6 
Javascript :: JavaScript Comparison and Logical Operators 
Javascript :: jQuery - Set 
Javascript :: TypeError: _enzymeAdapterReact.EnzymeAdapter is not a constructor 
Javascript :: adding transition to collapse button js 
Javascript :: phaser muy bridge 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =