Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Material UI + NextJS: "Prop did not match. Server: Client:" #4249

Closed
kutsyk opened this issue May 2, 2018 · 21 comments
Closed

Using Material UI + NextJS: "Prop did not match. Server: Client:" #4249

kutsyk opened this issue May 2, 2018 · 21 comments

Comments

@kutsyk
Copy link

kutsyk commented May 2, 2018

Expected Behavior

Works without problem.

Current Behavior

Errors with "material-ui": "^1.0.0-beta.44" and "next": "^6.0.0" tried a lot of configuration with .babelrc doesn't work as expected.
When I'm using material-ui component MuiThemeProvider and refresh page using browser refresh not all component are rendered properly and in console there is next warning:
Warning: Prop 'className' did not match. Server: "MuiTypography-root-246 MuiTypography-title-252 MuiTypography-colorInherit-265 PageComponent-flex-207" Client: "MuiTypography-root-44 MuiTypography-title-50 MuiTypography-colorInherit-63 PageComponent-flex-5"

Steps to Reproduce (for bugs)

Page component wich is container for whole app:

import React from 'react'
import PropTypes from 'prop-types'
import Head from 'next/head'
import Document from 'next/document'
import NavigationBottom from './navigation/bottom'
import { MuiThemeProvider, createMuiTheme, withStyles } from 'material-ui/styles'
import { Hidden } from 'material-ui'

import AppBar from 'material-ui/AppBar'
import Toolbar from 'material-ui/Toolbar'
import Typography from 'material-ui/Typography'
import IconButton from 'material-ui/IconButton'
import Link from 'next/link'
import Drawer from 'material-ui/Drawer'
import Divider from 'material-ui/Divider'
import List, { ListItem, ListItemIcon, ListItemText } from 'material-ui/List';
import { inherits } from 'util'

import { MenuList, MenuItem } from 'material-ui/Menu';
import PeopleIcon from '@material-ui/icons/People'
import FavoriteIcon from '@material-ui/icons/Favorite'
import Help from '@material-ui/icons/Help'
import blue from 'material-ui/colors/blue';

const muiTheme = createMuiTheme({
  palette: {
    primary: {
      main: '#32be9e'
    }
  }
})

const drawerWidth = 210

const styles = theme => ({
  root: {
    flexGrow: 1,
    zIndex: 1,
    overflow: 'hidden',
    position: 'relative',
    display: 'flex',
  },
  appBar: {
    zIndex: theme.zIndex.drawer + 1,
    color: '#32be9e',
    backgroundColor: 'white'
  },
  drawerPaper: {
    position: 'relative',
    width: drawerWidth,
  },
  content: {
    flexGrow: 1,
    backgroundColor: theme.palette.background.default,
    padding: theme.spacing.unit * 3,
    minWidth: 0, // So the Typography noWrap works
  },
  flex: {
    flex: 1
  },
  toolbar: theme.mixins.toolbar,
});

class PageComponent extends Document {
  static propTypes = {
    isServer: PropTypes.bool,
    title: PropTypes.string.isRequired,
    children: PropTypes.element.isRequired,
    classes: PropTypes.object.isRequired
  }

  static defaultProps = {
    navigationVisible: true
  }

  computedHeadTitle() {
    return this.props.title ? `${this.props.title} | Cureety` : 'Cureety'
  }

  render() {
    let { classes, navigationVisible } = this.props
    return (
      <MuiThemeProvider theme={createMuiTheme({
        palette: {
          primary: blue,
        },
      })} >
        <Head>
          <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" />
          <meta name="viewport" content="initial-scale=1.0, width=device-width" />
          <title>{this.computedHeadTitle()}</title>
          <link rel='stylesheet' href='/static/custom.css' />
        </Head>
        <div className={classes.root}>
          <AppBar position="absolute" className={classes.appBar}>
            <Toolbar>
              <Typography variant="title" color="inherit" className={classes.flex}>
                <Link href="/">
                  <a className="link-styleless">Cureety</a>
                </Link>
              </Typography>
              <Link href="/help">
                <IconButton
                  aria-haspopup="true"
                  color="inherit"
                  tooltip="Assistance">
                  <Help />
                </IconButton>
              </Link>
            </Toolbar>
          </AppBar>
          <Hidden only={['xs']}>
            {navigationVisible > 0 &&
              <Drawer
                variant="permanent"
                classes={{
                  paper: classes.drawerPaper,
                }}>
                <div className={classes.toolbar} />
                <MenuList>
                  <Link href="/suivi">
                    <MenuItem>
                      <ListItemIcon>
                        <FavoriteIcon />
                      </ListItemIcon>
                      <ListItemText inset primary="Mon suivi" />
                    </MenuItem>
                  </Link>
                  <Link href="/profile">
                    <MenuItem>
                      <ListItemIcon>
                        <PeopleIcon />
                      </ListItemIcon>
                      <ListItemText inset primary="Mon profile" />
                    </MenuItem>
                  </Link>
                </MenuList>
                <Divider />
                <List></List>
              </Drawer>
            }
          </Hidden>

          <main className={classes.content}>
            <div className={classes.toolbar} />
            <Typography noWrap component="div">
              <div>
                {this.props.children}
              </div>
            </Typography>
          </main>
          {navigationVisible > 0 &&
            <Hidden only={['sm', 'md', 'lg', 'xl']}>
              <NavigationBottom />
            </Hidden>
          }
        </div>
      </MuiThemeProvider>
    )
  }
}

const Page = withStyles(styles)(PageComponent)

export default Page

Context

Your Environment

Tech Version
next ^6.0.0
node v8.9.3
OS Windows 10
browser Chrome Version 66.0.3359.139 (Build officiel) (64 bits)
etc

Connected issue, said to move question to another issue: #3983

@fredsh
Copy link

fredsh commented May 4, 2018

Hello, I am facing the same issue.
If that can help for investigation:

  • the problem appears only in dev mode (works fine in production build)
  • there is a problem only if the theme provider is inside the new _app.js file (if I create a wrapper HOC and put it around each page instead there is no issue)

@oliviertassinari
Copy link
Contributor

oliviertassinari commented May 4, 2018

Did you start from https://github.com/mui-org/material-ui/tree/master/examples/nextjs?

@fredsh
Copy link

fredsh commented May 4, 2018

@oliviertassinari thank you for your reply. yes I actually used the wrapper withRoot from the example before v6 was released.
The new _app.js seems like the right place for applying the theme to all pages so I move the logic in there. It actually works well except for the initial render from server. The classnames are not matching and therefore the app is not styled. If a state change is triggered inside _app, then the re-render has correct classnames and style is applied correctly.
I was not facing any issue before moving the logic to _app.js
From my understanding, _app.js seems like the right place to do this as it applies to all pages.
Is there something that would need to be changed compared to the example to get it working in _app?

I would be glad to help fix the issue if I can but I am not quite sure where to start looking at for debugging this.

@oliviertassinari
Copy link
Contributor

@fredsh Follow mui/material-ui#11209 then.

@mpint
Copy link

mpint commented May 7, 2018

I was stuck on this for some time because I had overlooked the (very important) getInitialProps method in https://github.com/mui-org/material-ui/blob/v1-beta/examples/nextjs/pages/_document.js. Don't be like me.

@Tapppi
Copy link

Tapppi commented May 7, 2018

Although @mpint did indeed find the reason for the error, there still remains the flickering issue @fredsh mentioned when using _app.js to inject MuiThemeProvider and styles. Fix is in #4288

@kutsyk
Copy link
Author

kutsyk commented May 8, 2018

Hey guys,
I fixed my solution adding implementation as in example.

Everything worked.
I enabled ssr: true again in my babel configurations and overided _document.js after that no problems with styles.
I have not implemented logic withRoot in my solution, I am just using my component as root element for pages. Is it bad for performance?

@Tapppi
Copy link

Tapppi commented May 8, 2018

@kutsyk withRoot is just a Higher Order Component (HOC) to isolate the logic in a single place to keep your code DRY and ease maintenance. I would advice you to use a HOC unless you have a compelling reason not to, but it should not affect performance.

@Tevinthuku
Copy link
Contributor

Im still getting the same issue. Any recommended fix

@gleb-svechnikov
Copy link

@kutsyk link to the example returns 404. Could you provide working example?

@timneutkens
Copy link
Member

https://github.com/mui-org/material-ui/tree/master/examples/nextjs

@timneutkens
Copy link
Member

Let's close this issue as it has been solved #4249 (comment)

zbycz added a commit to zbycz/osmapp that referenced this issue Jan 19, 2019
Otherwise: Warning: Prop `className` did not match. Server: "MuiSvgIcon-root-53" Client: "MuiSvgIcon-root-1"
vercel/next.js#4249 (comment)
zbycz added a commit to zbycz/osmapp that referenced this issue Jan 19, 2019
Otherwise: Warning: Prop `className` did not match. Server: "MuiSvgIcon-root-53" Client: "MuiSvgIcon-root-1"
vercel/next.js#4249 (comment)
zbycz added a commit to zbycz/osmapp that referenced this issue Jan 24, 2019
Otherwise: Warning: Prop `className` did not match. Server: "MuiSvgIcon-root-53" Client: "MuiSvgIcon-root-1"
vercel/next.js#4249 (comment)
zbycz added a commit to zbycz/osmapp that referenced this issue Jan 29, 2019
Otherwise: Warning: Prop `className` did not match. Server: "MuiSvgIcon-root-53" Client: "MuiSvgIcon-root-1"
vercel/next.js#4249 (comment)
@offero
Copy link

offero commented Feb 22, 2019

I would love to see a write up explaining what is going on with the nextjs example. _app.js, _document.js, and getPageContext.js files are all custom built to make sure Material UI can be themed properly in next.

I'm try to learn next, react, and material-ui and the things that are happening in those files are like magic at the moment.

@oliviertassinari
Copy link
Contributor

@offero I personally wish we could wrap all the logic in a plugin so people don't have to worry about it.

@garrett-thompson
Copy link

Any update on this? I really have no idea what's going on in that example. It seems this is only a problem when using the Next dev server when loading changes.

@ghost

This comment has been minimized.

@cabralada
Copy link

Hey guys,
I fixed my solution adding implementation as in example .

Everything worked.
I enabled ssr: true again in my babel configurations and overided _document.js after that no problems with styles.
I have not implemented logic withRoot in my solution, I am just using my component as root element for pages. Is it bad for performance?

page not found!

@timneutkens
Copy link
Member

@edgarcheverier
Copy link

Following this article, I managed to make it work but isn't straight forward, I hope it can help someone.
https://medium.com/manato/ssr-with-next-js-styled-components-and-material-ui-b1e88ac11dfa

@francisrod01
Copy link
Contributor

francisrod01 commented Jul 28, 2020

I can't see this solved even with the simplistic material-ui example for next.js apps.

This issue occurs only when I enable the reactStrictMode in a next.config.js file.
If I rename this file, the app will run without any errors in styles, but I need this to set my environment variables before the application running.

https://github.com/mui-org/material-ui/tree/master/examples/nextjs

Here's a preview of the provided app's example in StrictMode.

Related:

image

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests