问题描述
我知道这个问题的堆栈溢出问题已经过时了,例如 React + Material-UI - 警告:Prop className 不匹配.
I know there are already dated versions of this question on stack overflow, like React + Material-UI - Warning: Prop className did not match.
但是,当我尝试用谷歌搜索和研究人们的解决方案时,并没有明确的答案.我能找到的任何答案都与我的堆栈不匹配.
However, when I attempt to google and research people's solutions, there is just no clear answer. Any answers I could find don't match my stack.
我的堆栈:
- 节点 JS
- 下一个 JS
- 材质界面
从我可以从 next.js &material-ui - 让它们工作是在涉及到 Next JS 和 Material UI 时存在某种程度的不兼容.
And from what I could glean from answers to questions like next.js & material-ui - getting them to work is that there is some measure of incompatibility when it comes to Next JS and Material UI.
代码方面,这是我的 Appbar 组件.最初我没有导出我的 useStyles
对象,但我最终做了一个可怜的尝试来跟随 Material UI 的服务器渲染"快速指南.必须有一个不涉及更改的修复程序,就像我拥有的每个文件一样.
Code-wise, here is my Appbar component. Initially I was not exporting my useStyles
object, but I ended up doing it in a pitiful attempt to follow along with Material UI's express guide to "server rendering". There has to be a fix that doesn't involve changing like every file I have.
import React from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import IconButton from '@material-ui/core/IconButton';
import Typography from '@material-ui/core/Typography';
import InputBase from '@material-ui/core/InputBase';
import { fade } from '@material-ui/core/styles/colorManipulator';
import { makeStyles } from '@material-ui/core/styles';
import MenuIcon from '@material-ui/icons/Menu';
import SearchIcon from '@material-ui/icons/Search';
import {connectSearchBox} from 'react-instantsearch-dom';
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
},
menuButton: {
marginRight: theme.spacing(2),
},
title: {
flexGrow: 1,
display: 'none',
[theme.breakpoints.up('sm')]: {
display: 'block',
},
},
search: {
position: 'relative',
borderRadius: theme.shape.borderRadius,
backgroundColor: fade(theme.palette.common.white, 0.15),
'&:hover': {
backgroundColor: fade(theme.palette.common.white, 0.25),
},
marginLeft: 0,
width: '100%',
[theme.breakpoints.up('sm')]: {
marginLeft: theme.spacing(1),
width: 'auto',
},
},
searchIcon: {
width: theme.spacing(7),
height: '100%',
position: 'absolute',
pointerEvents: 'none',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
inputRoot: {
color: 'inherit',
},
inputInput: {
padding: theme.spacing(1, 1, 1, 7),
transition: theme.transitions.create('width'),
width: '100%',
[theme.breakpoints.up('sm')]: {
width: 300,
'&:focus': {
width: 400,
},
},
}
}));
function SearchBox({currentRefinement, refine}){
const classes = useStyles();
return(
<InputBase
type="search"
value={currentRefinement}
onChange={event => refine(event.currentTarget.value)}
placeholder="Search by state, park name, keywords..."
classes = {{
root: classes.inputRoot,
input: classes.inputInput,
}}
/>
)
}
const CustomSearchBox = connectSearchBox(SearchBox);
function SearchAppBar() {
const classes = useStyles();
return (
<div className={classes.root}>
<AppBar position="static" color="primary">
<Toolbar>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="Open drawer"
>
<MenuIcon />
</IconButton>
<Typography className={classes.title} variant="h6" noWrap>
Title
</Typography>
<div className={classes.search}>
<div className={classes.searchIcon}>
<SearchIcon />
</div>
<CustomSearchBox/>
</div>
</Toolbar>
</AppBar>
</div>
);
}
export {SearchAppBar, useStyles};
推荐答案
我只是在互联网的随机部分中寻找这个错误的答案,不小心 npm install
'ed styled-components
作为这个答案的一部分在 Github 问题上(因为它们与 Material UI 中的对应对象非常相似,称为 ServerStyleSheet
(与 Material UI 的 ServerStyleSheets
相比),所以显然这不起作用.
I was just digging around random parts of the internet looking for answers to this error, accidentally npm install
'ed styled-components
as part of this answer on a Github issue (because they have a very similar object to the counterpart in Material UI called ServerStyleSheet
(vs Material UI's ServerStyleSheets
), so obviously that didn't work.
但是.........我最终只是使用 ServerStyleSheet
修复来尝试使其与 Material UI 的 ServerStyleSheets
对象,并以这个新的 _document.js
结束.
BUT......... I ended up just using the ServerStyleSheet
fix to try to make it agreeable with Material UI's ServerStyleSheets
object, and ended up with this new _document.js
.
我仍然目瞪口呆,我能够重构一个完全不同的修复程序来完成这项工作,但我对其进行了测试,它完全解决了问题,现在重新加载没问题.
I'm still dumbfounded I was able to refactor an entirely different fix to make this work but I tested it and it fixes the problem entirely, now reloads are fine.
import Document, { Html, Head, Main, NextScript } from 'next/document';
import {ServerStyleSheets} from "@material-ui/styles";
class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
try{
ctx.renderPage = () => originalRenderPage({
enhanceApp: App => props => sheet.collect(<App {...props}/>)
});
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
}
} finally {
ctx.renderPage(sheet)
}
}
render() {
return (
<Html>
<Head>
<link rel="shortcut icon" type="image/png" href="../static/favicon.ico"/>
<style>{`body { margin: 0 } /* custom! */`}</style>
<meta name="viewport"content="width=device-width, initial-scale=1.0" />
</Head>
<body className="custom_class">
<Main />
<NextScript />
</body>
</Html>
)}
}
export default MyDocument;
如果你想看看它有多疯狂,这里是 styled-components
中相同错误的修复:
If you wanna see how crazy it was that it worked, here is the fix for the same error in styled-components
:
导出默认的我的文档;
import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'
export default class MyDocument extends Document {
static async getInitialProps (ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheet.collectStyles(<App {...props} />)
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
}
} finally {
sheet.seal()
}
}
}
我希望这有助于解决 Material-UI + Next.js 的混乱情况
I hope this helped someone with the mess that is Material-UI + Next.js
这篇关于警告:道具 `className` 不匹配 ~ Material UI css 在重新加载时任意中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!