【问题标题】:Background images for dynamically rendered components not appearing on first render动态渲染组件的背景图像未出现在第一次渲染时
【发布时间】:2020-10-04 11:07:40
【问题描述】:

编辑:我通过在 Home 函数中动态导入 <Card> 组件来解决此问题:

 const Card = dynamic(() => import("@material-ui/core/Card"), {
    loading: () => <div>Loading...</div>,
  });

由于某种原因,当我使用背景图像(&lt;CardMedia&gt; 组件)动态渲染 Material UI 卡片时,图像不会出现在第一次渲染中。当我刷新页面时,它们也没有显示。

但是,当通过npm run dev 重建页面时(修改并保存文件后),图像似乎只会在页面再次刷新时消失!

如果我将源代码用作&lt;img&gt;,它们会出现在第一次渲染和随后的刷新中。所以它必须与 Material UI 或背景图像的 SSR 有关。

有人有想法吗?

PS。它与 Apollo 无关,我使用的是从我的目录中的 JSON 文件导入的数据。

import Layout from "../components/Layout";
import Head from "next/head";
import Link from "next/link";
import items from "../seeds";
import { makeStyles } from "@material-ui/core/styles";
import { withApollo } from "../lib/apollo";
import { useQuery } from "@apollo/react-hooks";

//GQL
import gql from "graphql-tag";

//CARD IMPORTS
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 Typography from "@material-ui/core/Typography";

//CARD STYLES
const useStyles = makeStyles({
  card: {
    maxWidth: "100%",
    width: 270,
    margin: "1rem",
    textAlign: "center",
  },
  media: {
    height: 0,
    paddingTop: "56.25%",
  },
});

const HELLO_QUERY = gql`
  query HelloQuery {
    sayHello
  }
`;

function Home() {
  const classes = useStyles();
  const MAX_LENGTH = 125;

  const { data, loading, error } = useQuery(HELLO_QUERY);

  if (loading) {
    return <div />;
  }
  console.log(data);

  return (
    <div>
      <Layout>
        <Typography variant="h2" align="center">
          {data.sayHello}
        </Typography>
        <Link href="/about">
          <a>About</a>
        </Link>
        <div
          style={{
            display: "flex",
            flexWrap: "wrap",
            justifyContent: "center",
          }}
        >
          {items.map((item: any) => {
            return (
              <Card className={classes.card} key={item._id}>
                <CardActionArea>
                  <Link href={`/event/${item._id}`}>
                    <CardMedia
                      wide
                      className={classes.media}
                      image={item.image}
                      title={`${item.name} image`}
                    />
                  </Link>
                  <CardContent>
                    <Typography gutterBottom variant="h5" component="h2">
                      {item.name}
                    </Typography>
                    <Typography
                      variant="body2"
                      color="textSecondary"
                      component="p"
                    >
                      {`${item.description.substring(0, MAX_LENGTH)}...`}
                    </Typography>
                  </CardContent>
                </CardActionArea>
              </Card>
            );
          })}
        </div>
      </Layout>
    </div>
  );
}

export default withApollo(Home);

【问题讨论】:

    标签: javascript reactjs material-ui next.js server-side-rendering


    【解决方案1】:

    我通过在 Home 函数中动态导入 &lt;Card&gt; 组件来解决此问题:

     const Card = dynamic(() => import("@material-ui/core/Card"), {
        loading: () => <div>Loading...</div>,
      });
    

    【讨论】:

      【解决方案2】:

      这可能有多种原因,因为我们看不到查询的有效负载,所以很难预测

      一些需要调试的地方: 1.你的反应是否一致

      1. 我将您的使用与我的一个项目实现进行了比较,我这边唯一的补充是在 CardMedia 中指定 component={'img'}

      2. 此外,除了将代码包装在 link 中之外,是否可以按照此处的建议使用 cardActions API - How to make the whole Card component clickable in Material UI using React JS?

      【讨论】:

      • 嘿@Ramakay。我将component={"img"} 添加为属性,并且图像在卡片中显示为未格式化(所有尺寸都太大且尺寸不同)。我认为添加component={"img"} 的问题在于它将其从背景图像转换为实际图像,这意味着height: 0, paddingTop: "56.25%" 没有创建(或保留)比例方面,并且卡片的尺寸正在被更改图片的默认尺寸。
      • 嘿@Ramkay,我解决了这个问题。查看我在原始帖子中的编辑^
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2023-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多