![](/img/trans.png)
[英]Material UI v5 server side rendering css order not working with gatsby
[英]Server side rendering with Material UI 1 and Nodejs/React
問題:似乎無法通過ssr獲取發送或加載的樣式。
我正在關注文檔並嘗試按照它的說法進行操作,但styleSheets變量似乎仍然是空的。 在我的導航組件中,我使用JSS並使用withStyles。 從閱讀一些文檔,使用withStyles應該在我的服務器上填充我的樣式表嗎? 但是,由於某些原因,保存這些樣式的變量最終會變空。 我誤解了什么嗎?
serverRender.jsx
import React from 'react';
import { renderToString } from 'react-dom/server';
import { Provider } from 'react-redux';
import { RouterContext } from 'react-router';
import Helmet from 'react-helmet';
import serialize from 'serialize-javascript';
import { create } from 'jss';
import preset from 'jss-preset-default';
import { SheetsRegistry } from 'react-jss/lib/jss';
import JssProvider from 'react-jss/lib/JssProvider';
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
import createGenerateClassName from 'material-ui/styles/createGenerateClassName';
import { deepOrange, lightBlue, red } from 'material-ui/colors';
import staticAssets from './static-assets';
// import rtl from 'jss-rtl'; // in-case you're supporting rtl
const sheetsRegistry = new SheetsRegistry();
const theme = createMuiTheme({
palette: {
primary: deepOrange,
secondary: lightBlue,
error: red,
},
});
// Configure JSS
const jss = create(preset());
jss.options.createGenerateClassName = createGenerateClassName;
const createApp = (store, props) => renderToString(
<JssProvider registry={sheetsRegistry} jss={jss}>
<MuiThemeProvider theme={theme} sheetsManager={new Map()}>
<Provider store={store}>
<RouterContext {...props} />
</Provider>
</MuiThemeProvider>
</JssProvider>
);
//Grab our css from our sheetsRegistry
const registeredCss = sheetsRegistry.toString();
const buildPage = ({ componentHTML, initialState, headAssets, css }) => {
return `
<!doctype html>
<html>
<head>
${headAssets.title.toString()}
${headAssets.meta.toString()}
${headAssets.link.toString()}
${staticAssets.createStylesheets()}
${staticAssets.createTrackingScript()}
</head>
<body>
<div id="app">${componentHTML}</div>
<script>window.__INITIAL_STATE__ = ${serialize(initialState)}</script>
${staticAssets.createAppScript()}
<style id="jss-server-side" type="text/css">${css}</style>
</body>
</html>`;
};
export default (store, props) => {
const initialState = store.getState();
const componentHTML = createApp(store, props);
const headAssets = Helmet.renderStatic();
const css = registeredCss;
console.log(css);
return buildPage({ componentHTML, initialState, headAssets, css });
};
這里應該抓住我們需要造型的地方。
導航可能看起來像這樣:
import React, { Component } from 'react';
import { Link } from 'react-router';
import classNames from 'classnames/bind';
import AppBar from 'material-ui/AppBar';
import Toolbar from 'material-ui/Toolbar';
import Typography from 'material-ui/Typography';
import Button from 'material-ui/Button';
import IconButton from 'material-ui/IconButton';
import MenuIcon from 'material-ui-icons/Menu';
import { withStyles } from 'material-ui/styles';
import styles from '../css/components/navigation.css';
// import passTheAuxLogo from '../images/PassTheAux.png';
const styleSheet = ({
appbar: {
width: '100%',
},
flex: {
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
navCenter: {
display: 'flex',
flex: 1
},
});
const cx = classNames.bind(styles);
class Navigation extends Component {
constructor(props) {
super(props);
this.mobile = false;
}
render() {
const mobile = this.mobile;
const { classes } = this.props;
return (
<div className={classes.appbar}>
<AppBar position="static" color="primary">
<Toolbar>
{mobile && (
<IconButton className={classes.menuButton} color="contrast" aria-label="Menu">
<MenuIcon />
</IconButton>)}
<Typography type="title" color="inherit" className={cx('flex')}>
PassTheAux
</Typography>
<div className={classes.navCenter}>
<Link to="/dashboard">
<Button color="contrast">Dashboard</Button>
</Link>
<Link to="/about">
<Button color="contrast">About</Button>
</Link>
<Link to="/404notfound">
<Button color="contrast">404 Not Found</Button>
</Link>
</div>
<Button color="contrast">Log In</Button>
|
<Button color="contrast">Sign up</Button>
</Toolbar>
</AppBar>
</div>
);
}
}
export default withStyles(styleSheet)(Navigation);
提供商是否應該繞過我的應用程序以及客戶端? https://github.com/kkotwal94/KaranPRNB
編輯:我想在這個問題的另一個問題是我必須使用JSS,如果我想使用PostCSS,這可以做到嗎?
您需要確保遵守以下執行流程:
ReactDOM.renderToString()
sheetsRegistry.toString()
它不會像在GitHub存儲庫中那樣完成其他方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.