getImage正在返回undefined,因此我的GatsbyImage组件不会被渲染.

文件 struct :

  • src/页面/图库.js公司
  • src/images(has 12 photos named photo-01.jpg,photo-02.jpg,…)

我有以下代码(gallery.js):

import React from "react";
import Layout from "../components/Layout";
import { useStaticQuery, graphql } from "gatsby";
import { GatsbyImage, getImage } from "gatsby-plugin-image";

const Gallery = () => {
  const { gallery } = useStaticQuery(graphql`
    query {
      gallery: allFile(
        filter: {
          extension: { eq: "jpg" }
          absolutePath: { regex: "/images/" }
        }
      ) {
        nodes {
          id
          childImageSharp {
            fluid(maxWidth: 500, maxHeight: 500) {
              ...GatsbyImageSharpFluid_tracedSVG
            }
          }
        }
      }
    }
  `);

  return (
    <Layout>
      <div className="container py-5">
        <div className="row">
          <div className="col-12">
            <h1 className="text-gastby mb-4">Gallery</h1>
          </div>
        </div>
        <div className="row">
          {gallery.nodes.map((image) => (
            <div className="col-lg-3 col-md-4 col-sm-6 mb-3" key={image.id}>
              <GatsbyImage
                image={getImage(image.childImageSharp.fluid)}
                alt="Gallery"
              />
            </div>
          ))}
        </div>
      </div>
    </Layout>
  );
};

export default Gallery;

我有什么错?

推荐答案

问题是你混合了gatsby-image(从 gatsby 1到 gatsby 2)和gatsby-plugin-image(从 gatsby 3开始).第一个现在已被弃用.

当您查询fluidfixed个GraphQL node 时,很容易发现这一点,因为它们是由gatsby-image创建的,gatsby-image使用Img(from 'gatsby-image’)组件,该组件接受fluidfixed属性.

另一方面,gatsby-plugin-imagegatsbyImageData(不是fluidfixed)查询和相应的GatsbyImage(from 'gatsby-plugin-image')组件接受image属性.

在您的示例中,您正在查询应用于gatsby-plugin-image组件的gatsby-image struct ,这就是为什么它返回未定义的.

判断migration guide,但实际上您需要替换(并删除)对gatsby-image的所有引用,并安装所需的依赖项:

npm install gatsby-plugin-image gatsby-plugin-sharp gatsby-transformer-sharp

将其添加到您的gatsby-config.js:

module.exports = {
  plugins: [
    `gatsby-plugin-image`,
    `gatsby-plugin-sharp`,
    `gatsby-transformer-sharp`,
  ],
}

并将查询更改为类似以下内容:

const Gallery = () => {
  const { gallery } = useStaticQuery(graphql`
    query {
      gallery: allFile(
        filter: {
          extension: { eq: "jpg" }
          absolutePath: { regex: "/images/" }
        }
      ) {
        nodes {
          id
          childImageSharp {
            gatsbyImageData(layout: FIXED)
          }
        }
      }
    }
  `);

  return (
    <Layout>
      <div className="container py-5">
        <div className="row">
          <div className="col-12">
            <h1 className="text-gastby mb-4">Gallery</h1>
          </div>
        </div>
        <div className="row">
          {gallery.nodes.map((image) => (
            <div className="col-lg-3 col-md-4 col-sm-6 mb-3" key={image.id}>
              <GatsbyImage
                image={getImage(image.childImageSharp)}
                alt="Gallery"
              />
            </div>
          ))}
        </div>
      </div>
    </Layout>
  );
};

export default Gallery;

Double-check the query since the nodes and the structure may be different when applying 100.

请注意,getImage是一个助手函数,您可能不需要它,请考虑直接使用:

  <GatsbyImage
    image={image.childImageSharp.gatsbyImageData}
    alt="Gallery"
  />

Javascript相关问答推荐

根据总价格对航班优惠数组进行排序并检索前五个结果- Angular HTTP请求

D3多线图显示1线而不是3线

仅圆角的甜甜圈图表

我无法在NightWatch.js测试中获取完整的Chrome浏览器控制台日志(log)

Klaro与Angular的集成

GrapeJS -如何保存和加载自定义页面

如何解决CORS政策的问题

防止用户在selectizeInput中取消 Select 选项

扫描qr code后出错whatter—web.js

当运行d3示例代码时,没有显示任何内容

当Redux提供程序访问Reduxstore 时,可以安全地从Redux提供程序外部调用钩子?

单个HTML中的多个HTML文件

Reaction-SWR-无更新组件

在不扭曲纹理的情况下在顶点着色器中旋转UV

未捕获语法错误:Hello World中的令牌无效或意外

本地损坏的Java脚本

使用可配置项目创建网格

Chrome上的印度时区名S有问题吗?

ReactJS在类组件中更新上下文

ReactJS Sweep Line:优化SciChartJS性能,重用wasmContext进行多图表渲染