question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Warning: Prop `className` did not match. Server.... Typography component, prop based styles

See original GitHub issue

react-dom.development.js?61bb:530 Warning: Prop className did not match.

Server: “MuiTypography-root makeStyles-root-32 makeStyles-root-47 makeStyles-darkest-27 makeStyles-bodySmall-43 makeStyles-noTextTransform-41 MuiTypography-body1” Client: “MuiTypography-root makeStyles-root-32 makeStyles-root-48 makeStyles-darkest-27 makeStyles-bodySmall-43 makeStyles-noTextTransform-41 MuiTypography-body1”

I have tried the following so far to no avail:

  • configured runtime chunk in webpack, as we are code splitting at the route level,
optimization: {
    runtimeChunk: {
      name: 'app',
    },
  },
  • verified there is only one version of material-ui, the latest, 4.9.3

  • node environment is the same for both client and server

  • tried wrapping both the client and app in stylesprovider with a fresh createGenerateClassName:

server:

const sheets = new ServerStyleSheets();

      const generateClassName = createGenerateClassName({
        productionPrefix: 'tock',
      });

      const html = ReactDomServer.renderToString(
        sheets.collect(
          <Provider store={store}>
            <StaticRouter location={req.url} context={routerContext}>
              <StylesProvider generateClassName={generateClassName}>
                <Application />
              </StylesProvider>
            </StaticRouter>
          </Provider>
        )
      );

as well as ensuring that new ServerStyleSheets is created on every requuest.

client:

const generateClassName = createGenerateClassName({
    productionPrefix: 'tock',
  });
  try {
    (fullRender ? ReactDOM.render : ReactDOM.hydrate)(
      <StrictMode>
        <Provider store={store}>
          <Router history={tockHistory}>
            <StylesProvider generateClassName={generateClassName}>
              <Routes />
            </StylesProvider>
          </Router>
        </Provider>
      </StrictMode>,
      document.querySelector('#Root')
    );
  } catch (e) {
    // eslint-disable-next-line no-console
    console.error(e);
    throw e;
  }

followed the reference implementation: https://material-ui.com/guides/server-rendering/

this seems to only be happening in development mode. If I bundle for production and run the app, it’s working.

worked through: https://material-ui.com/getting-started/faq/#my-app-doesnt-render-correctly-on-the-server

specifically, I was interested in this: https://material-ui.com/getting-started/faq/#react-class-name-hydration-mismatch

Node: v13.8.0 Webpack: 4.41.6 Material-ui: 4.9.3

the component where we started seeing this issue, interestingly, uses some prop based styles:

export const useTypographyStyles = makeStyles((theme) => ({
  root: {
    marginBottom: ({ mb, paragraph }: TypographyProps) =>
      paragraph ? theme.spacing(2) : theme.spacing(mb ?? 0),
  },

which I notice generates 2 strings for the classes in the hook.

root: "makeStyles-root-32 makeStyles-root-232"

is what I get when I log the result of:

const classes = useTypographyStyles({ mb, paragraph });

but if I do a regular style there with just a value, I do not get the issue.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12

github_iconTop GitHub Comments

0reactions
kelly-tockcommented, Feb 19, 2020

only way i’ve been able to solve this for now is to not use props in styles on SSR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Warning: Prop `className` did not match. when using styled ...
That error is showing you the class that is being created by your styled-components library on the server and how it is different...
Read more >
Warning: Prop className did not match. #7322 - GitHub
It appears to be an issue that backend generates styled component but client doesn't get it/doesn't use it and instead of using server...
Read more >
Solving the Styled Components Warning in Next.JS with Babel
Warning : Prop `className` did not match. Server: “fs-fdsf” Client: “sd-dfasj”. The combination of Next.JS and Styled Components is truly ...
Read more >
Errors I dealt with in Next.js - styled components, pages, types
1. Warning : Props 'className' did not match Server: ' ' Client: ' '. I wanted to add styled components so I installed...
Read more >
Next.Js React app with styled components. Warning: Prop ...
This error means that something on the server is different from the Client. This can happen if the client does a re-render. Styled...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found