material-ui客户端警告:Prop`style`不匹配

时间:2017-09-29 18:37:34

标签: javascript node.js reactjs redux material-ui

我正在使用服务器端渲染设置react-redux应用程序,而我正在使用react material-ui。在初始呈现应用程序时,我面临以下警告

WARNING

以下是项目文件

服务器文件 index.js

'use strict';

import express from 'express';
import webpack from 'webpack';
import path from 'path';
import config from '../webpack.config.js';
import chalk from 'chalk';
import bodyParser from 'body-parser';
import handleRender from '../client/src/utils/handleRender.js';

// Tell any CSS tooling (such as Material UI) to use all vendor prefixes if the
// user agent is not known.
global.navigator = global.navigator || {};
global.navigator.userAgent = global.navigator.userAgent || 'all';

const port = 3000;
const app = express();

webpack(config).run((err, results) => {
  // console.log(err);
  // console.log(results);
});
// Middlewares
app.use(express.static(path.resolve(__dirname, '../build'), { maxAge: 30 * 60 * 60 * 24 * 1000 }));
app.use(bodyParser.json());
app.use(handleRender);

// Trigger server
app.listen(port, function(err) {
  if (err) {
    console.log(chalk.red("Whoa!!! Server crashed..."), err);
  } else {
    console.log(chalk.green("YAY!!! Server is up and running..."));
  }
});

客户端 index.js

'use strict';

import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import { hydrate } from 'react-dom';
import { Provider } from 'react-redux';
import { renderRoutes } from 'react-router-config';
import configureStore from './store';
import App from './containers/App.js';
import routes from './routes/index.js';

hydrate(
  <Provider store={configureStore()}>
    <BrowserRouter>
      {renderRoutes(routes)}
    </BrowserRouter>
  </Provider>,
  document.getElementById('root')
);

如果您需要任何其他信息,请与我们联系。在此先感谢!!!

1 个答案:

答案 0 :(得分:5)

material-ui生成带有供应商前缀的内联样式。它仅生成用户浏览器所需的供应商前缀。

global.navigator.userAgent = global.navigator.userAgent || 'all';

在服务器上,这变为userAgent = 'all' ...所以material-ui总是添加所有供应商前缀。然后在浏览器上它只添加它所需的供应商前缀,在现代浏览器中通常为0.这就是差异所在的地方。

要尝试解决此问题,请在服务器呈现应用之前将userAgent设置为服务器在请求中收到的服务器。见the material-ui docs on server rendering。您可以将req.headers['user-agent']传递给传递给MuiThemeProvider的muiTheme对象,或者将其设置在全局navigator对象上。