material-ui卡未加载CSS

时间:2019-05-29 20:20:19

标签: css reactjs material-ui

enter image description here

我有使用Material-UI创建的卡片。问题是当页面加载或重新加载时,卡片看起来像左侧的卡片。似乎没有任何CSS。几秒钟后,看起来应该看起来像。是因为我正在做某事还是只是Material-UI的工作方式

import React, { Component } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Card from '@material-ui/core/Card';
import CardActionArea from '@material-ui/core/CardActionArea';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';



class ImgMediaCard extends Component {
  constructor(props){
    super(props)
  }
  render(){

  return (
    <div className="Cards-div">
    <Card className="Cards">
      <CardActionArea>
        <CardMedia
          component="img"
          alt={this.props.imgAlt}
          height="300"
          image={this.props.imgLink}
          title={this.props.imgTitle}
        />
        <CardContent>
          <Typography gutterBottom variant="h5" component="h2">
            {this.props.title}
          </Typography>
          <Typography variant="body2" color="textSecondary" component="p">
            {this.props.text}
            {this.props.technologies}
          </Typography>
        </CardContent>
      </CardActionArea>
      <CardActions>
        <Button size="small" color="primary">
          Visit
        </Button>

      </CardActions>
    </Card>
  </div>
)};
}

export default ImgMediaCard;

Github存储库:https://github.com/Kohdz/port

1 个答案:

答案 0 :(得分:1)

好了一段时间,我终于弄明白了。密钥似乎在/pages/_document.js中。我需要做两件事,带回_document.js并让其处理getInitialProps()并从flush中添加styled-jsx

import React from 'react';
import Document, { Head, Main, NextScript } from 'next/document';
import { ServerStyleSheets } from '@material-ui/styles';
import flush from 'styled-jsx/server';
import { createMuiTheme } from '@material-ui/core/styles';
import red from '@material-ui/core/colors/red';

const theme = createMuiTheme({
  palette: {
    primary: {
      main: '#556cd6',
    },
    secondary: {
      main: '#19857b',
    },
    error: {
      main: red.A400,
    },
    background: {
      default: '#fff',
    },
  },
});

class MyDocument extends Document {

    render() {
        return (
            <html lang="en" dir="ltr">
                <Head>
                    <meta charSet="utf-8" />
                    <meta
                        name="viewport"
                        content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no"
                    />
                    <meta name="theme-color" content={theme.palette.primary.main} />
                    <link
                        rel="stylesheet"
                        href="https://fonts.googleapis.com/css?family=Roboto:300,400,500&display=swap"
                    />
                </Head>
                <body>
                    <Main />
                    <NextScript />
                </body>
            </html>
        );
    }
}

MyDocument.getInitialProps = async ctx => {

    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: (
            <React.Fragment>
                {sheets.getStyleElement()}
                {flush() || null}
            </React.Fragment>
        ),
    };
};

export default MyDocument;

Material UI has an example here. This was also an interesting thread