首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React-slick with gatsby-plugin-image

React-slick with gatsby-plugin-image
EN

Stack Overflow用户
提问于 2021-03-11 03:03:33
回答 1查看 1.1K关注 0票数 2

我正在尝试使用带有gatsby-plugin图片的React-slick,我的页面设置如下。

代码语言:javascript
复制
import React from "react";
import { graphql } from "gatsby"
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
import { GatsbyImage } from "gatsby-plugin-image"

const settings = {
  autoPlay: true,
  arrows: false,
  dots: true,
  infinite: true,
  speed: 500,
  slidesToShow: 1,
  slidesToScroll: 1,
};

const ImgSlide = ({ data }) => {
  return (
    <div>
      <Slider {...settings}>
        <div>
          <GatsbyImage fluid={data.image1.childImageSharp.fluid} />
        </div>
        <div>
        <GatsbyImage fluid={data.image2.childImageSharp.fluid} />
        </div>
      </Slider>
    </div>
  );
};

export const pageQuery = graphql`
  query {
    image1: file(relativePath: { eq: "images/icon.png" }) {
      childImageSharp {
        fluid {
          ...GatsbyImageSharpFluid
        }
      }
    }
    image2: file(relativePath: { eq: "images/icon.png" }) {
      childImageSharp {
        fluid {
          ...GatsbyImageSharpFluid
        }
      }
    }
  }
`

export default ImgSlide;

当我运行Gatsby develop时,我得到一个错误,说image1没有定义。我真的不知道我错过了什么。我认为这与我试图定义image1的方式有关,但我非常确定我已经正确地使用了relativePath,除非我没有正确地指定位置。

我确实指定了两次相同的图像,这只是因为我还没有导入照片,我只是在测试它的工作。

gatsby-config设置为

代码语言:javascript
复制
module.exports = {
  siteMetadata: {
    title: "Inkd Era",
    description: "Clothing and brand built for tattoo and tattoed culture",
  },
  plugins: [
    "gatsby-plugin-sass",
    "gatsby-plugin-image",
    "gatsby-plugin-react-helmet",
    "gatsby-plugin-sitemap",
    {
      resolve: "gatsby-plugin-manifest",
      options: {
        icon: "src/images/icon.png",
      },
    },
    "gatsby-transformer-remark",
    "gatsby-plugin-sharp",
    "gatsby-transformer-sharp",
    {
      resolve: "gatsby-transformer-remark",
      options: {
        plugins: [
          {
            resolve: "gatsby-remark-images",
            options: {
              maxWidth: 650,
            },
          },
        ],
      },
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "images",
        path: `${__dirname}/src/images/`,
      },
      __key: "images",
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "pages",
        path: `${__dirname}/src/pages/`,
      },
      __key: "pages",
    },
    {
    resolve: `gatsby-plugin-manifest`,
    options: {
      name: `Inkd Era`,
      short_name: `Inkd era`,
      start_url: `/`,
      background_color: `#000`,
      theme_color: `#fafafa`,
      display: `standalone`,
      icon: `content/assets/gatsby-icon.png`,
    },
  },
  ],
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-11 13:54:49

传递图像本身时,新<GatsbyImage>组件的结构使用的是image prop,而不是fluid。此外,查询需要获取gatsbyImageData,而不是您在docs中看到的fluid

代码语言:javascript
复制
import { graphql } from "gatsby"
import { GatsbyImage, getImage } from "gatsby-plugin-image"

function BlogPost({ data }) {
  const image = getImage(data.blogPost.avatar)
  return (
    <section>
      <h2>{data.blogPost.title}</h2>
      <GatsbyImage image={image} alt={data.blogPost.author} />
      <p>{data.blogPost.body}</p>
    </section>
  )
}

export const pageQuery = graphql`
  query {
    blogPost(id: { eq: $Id }) {
      title
      body
      author
      avatar {
        childImageSharp {
          gatsbyImageData(
            width: 200
            placeholder: BLURRED
            formats: [AUTO, WEBP, AVIF]
          )
        }
      }
    }
  }
`

在您的场景中,您混合了来自Gatsby v2gatsby-image方法和新的gatsby-plugin-image,它仍然处于测试阶段,但它来自v3

如果要使用<GatsbyImage>,请根据需要调整查询和组件,否则,请正确使用gatsby-image,如下所示:

代码语言:javascript
复制
import Img from `gatsby-image`

<Img fluid={data.image1.childImageSharp.fluid} />
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66571224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档