由于 classNames 的分配方式不同,我很难处理 Material-UI 组件中客户端和服务器端样式呈现之间的差异。
classNames 在第一次加载页面时被正确分配,但在刷新页面后,classNames 不再匹配,因此组件失去了它的样式。这是我在控制台上收到的错误消息:
Warning: Prop
className
did not match. Server: "MuiFormControl-root-3 MuiFormControl-marginNormal-4 SearchBar-textField-31" Client: "MuiFormControl-root-3 MuiFormControl-marginNormal-4 SearchBar-textField-2"
我已经关注了 Material-UI TextField example docs , 以及它们的伴随 Code Sandbox example ,但我似乎无法弄清楚是什么导致了服务器和客户端类名之间的差异。
我在添加带有删除“x”图标的 Material-UI 芯片时遇到了类似的问题。刷新后呈现出 1024px 宽度的“x”图标。同样的潜在问题是该图标没有收到正确的样式类。
Stack Overflow 上有几个问题解决了为什么客户端和服务器可能会以不同方式呈现类名(例如,需要升级到 @Material-UI/core 版本 ^1.0.0,使用自定义 server.js,并使用 Math.在 setState 中是随机的),但这些都不适用于我的情况。
我不知道是否this Github discussion可能会有帮助,但可能不会,因为他们使用的是测试版的 Material-UI。
重现的最少步骤:
创建项目文件夹并启动 Node 服务器:
mkdir app
cd app
npm init -y
npm install react react-dom next @material-ui/core
npm run dev
编辑 package.json:
添加到'脚本':"dev": "next",
应用/页面/index.jsx:
import Head from "next/head"
import CssBaseline from "@material-ui/core/CssBaseline"
import SearchBar from "../components/SearchBar"
const Index = () => (
<React.Fragment>
<Head>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"
/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charSet="utf-8" />
</Head>
<CssBaseline />
<SearchBar />
</React.Fragment>
)
export default Index
应用/组件/SearchBar.jsx:
import PropTypes from "prop-types"
import { withStyles } from "@material-ui/core/styles"
import TextField from "@material-ui/core/TextField"
const styles = (theme) => ({
container: {
display: "flex",
flexWrap: "wrap",
},
textField: {
margin: theme.spacing.unit / 2,
width: 200,
border: "2px solid red",
},
})
class SearchBar extends React.Component {
constructor(props) {
super(props)
this.state = { value: "" }
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
handleChange(event) {
this.setState({ value: event.target.value })
}
handleSubmit(event) {
event.preventDefault()
}
render() {
const { classes } = this.props
return (
<form
className={classes.container}
noValidate
autoComplete="off"
onSubmit={this.handleSubmit}
>
<TextField
id="search"
label="Search"
type="search"
placeholder="Search..."
className={classes.textField}
value={this.state.value}
onChange={this.handleChange}
margin="normal"
/>
</form>
)
}
}
SearchBar.propTypes = {
classes: PropTypes.object.isRequired,
}
export default withStyles(styles)(SearchBar)
在浏览器中访问页面 localhost:3000
并看到这个:
red border around TextField component
刷新浏览器,看到这个:
TextField component's styles are gone
请注意,TextField 周围的红色边框消失了。
相关库:
- “ react ”:16.4.0
- “ react 域”:16.4.0
- “下一个”:6.0.3
- “@material-ui/core”:1.2.0
最佳答案
问题出在Next.js中的SSR渲染,会在页面渲染之前产生样式片段。
使用 Material UI 和 Next.js(作者正在使用),添加一个名为 _document.js
的文件解决了这个问题。
调整后的 _document.js
( as suggested here ):
import React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheets } from '@material-ui/styles'; // works with @material-ui/core/styles, if you prefer to use it.
import theme from '../src/theme'; // Adjust here as well
export default class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head>
{/* Not exactly required, but this is the PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
// Resolution order
//
// On the server:
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. document.getInitialProps
// 4. app.render
// 5. page.render
// 6. document.render
//
// On the server with error:
// 1. document.getInitialProps
// 2. app.render
// 3. page.render
// 4. document.render
//
// On the client
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. app.render
// 4. page.render
// Render app and page and get the context of the page with collected side effects.
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
};
};
关于node.js - React + Material-UI - 警告 : Prop className did not match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50685175/