ark*_*k0n 4 javascript css reactjs material-ui next.js
我知道关于堆栈溢出的这个问题已经有过时的版本,比如React + Material-UI - Warning: Prop className did not match。
但是,当我尝试 google 并研究人们的解决方案时,没有明确的答案。我能找到的任何答案都与我的堆栈不匹配。
我的堆栈:
从我可以从next.js 和 material-ui等问题的答案中收集到的信息- 让它们工作是在 Next JS 和 Material UI 方面存在某种程度的不兼容。
代码方面,这是我的 Appbar 组件。最初我没有导出我的useStyles
对象,但我最终做了一个可怜的尝试,遵循Material UI 的“服务器渲染”明确指南。必须有一个不涉及更改的修复程序,就像我拥有的每个文件一样。
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};
Run Code Online (Sandbox Code Playgroud)
我只是挖在网上寻找答案,这样错误的周围随机配件,不小心npm install
“编styled-components
为部分这个答案在Github上的问题(因为他们有一个非常类似的对象在材质对方UI叫ServerStyleSheet
(VS材料UI的ServerStyleSheets
)很明显那没有用。
但是......我最终只是使用ServerStyleSheet
修复程序来尝试使其与 Material UI 的ServerStyleSheets
对象一致,并最终得到了这个新的_document.js
.
我仍然目瞪口呆,我能够重构一个完全不同的修复程序来完成这项工作,但我对其进行了测试,它完全解决了问题,现在重新加载没问题。
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;
Run Code Online (Sandbox Code Playgroud)
如果你想看看它的工作有多疯狂,这里是对相同错误的修复styled-components
:
导出默认的 MyDocument;
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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这能帮助解决 Material-UI + Next.js 的人
就我而言,添加{ name: "MuiExample_Component" }
makeStyle 挂钩是出于某种原因。我在互联网上挖掘时找到了这个解决方案。如果有人能告诉我这是否是一个好的解决方案,我将不胜感激,但这是代码:
const useStyles = makeStyles({
card: {
backgroundColor: "#f7f7f7",
width: "33%",
},
title: {
color: "#0ab5db",
fontWeight: "bold",
},
description: {
fontSize: "1em"
}
}, { name: "MuiExample_Component" });
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5216 次 |
最近记录: |