Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

NextJs + Material UI, manually refreshing causes

//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 :: vue2-datepicker nuxtjs example 
Javascript :: wp include js 
Javascript :: winston transport file another directory 
Javascript :: How to Loop Through an Array with a While Loop in JavaScript 
Javascript :: Backbone Notes Miscellaneous 
Javascript :: expact 
Javascript :: react creating function to call API in app: calling APIs after render w error message 
Javascript :: how to get on hnage input before clicking off 
Javascript :: dockerignore node_modules 
Javascript :: country select dropdown javascript 
Javascript :: document.getelementbyid add number 
Javascript :: create number format excel react native 
Javascript :: array of objects javascript 
Javascript :: empty an array in javascript 
Javascript :: Closure examples 
Javascript :: use of prototype in javascript 
Javascript :: dynamic styles in react native 
Javascript :: mongoose schema for nested items 
Javascript :: arrow expression javascript 
Javascript :: events js 
Javascript :: JavaScript (SMonkey 60.2.3) sample 
Javascript :: mdn objects javascript 
Javascript :: findPotentialLikes javascript 
Javascript :: JavaScript built-in methods 
Javascript :: javascript Set Subset Operation 
Javascript :: javascript Number() Method Used on Dates 
Javascript :: jquery callback functions 
Javascript :: js regex find newlines 
Javascript :: regex tunisian phone number 
Javascript :: phaser multi atlas animation 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =