javascript - 如何使用 Contentful 在 Gatsby 中包含轮播组件?

标签 javascript html graphql gatsby contentful

我想在我的 Gatsby 项目中包含一个图像轮播。所有图片都将从 Contentful 查询。

我使用了“react-responsive-carousel”,并设法让它与直接导入图像一起工作。我也确信我可以成功地提取内容丰富的图像,而无需将其放入轮播组件中。

谁能帮我解决这个问题?似乎 gatsby-image 组件 Img 没有返回“react-reponsive-carousel”可以识别的标签。或者你知道任何其他可以工作的轮播吗?

我的代码:

import React, { Component, Fragment } from "react"

import "react-responsive-carousel/lib/styles/carousel.min.css"
import { Carousel } from "react-responsive-carousel"
import { graphql } from "gatsby"
import Img from "gatsby-image"

import tourimg from "../images/tour-1.jpg"  //Import an image directly

export default class ApartmentPage extends Component {
  render() {
    const photos = this.props.data.allContentfulRooms.edges[0].node.photos
    return (
      <Carousel>
        {photos.map(photo => {
          const { fixed } = photo
          return (
            <div>
              <Img fixed={fixed} />
              {/* <img src={tourimg} /> */}
            </div>
          )
        })}
      </Carousel>
    )
  }
}

export const ROOM_QUERY = graphql`
  {
    allContentfulRooms {
      edges {
        node {
          id
          name
          photos {
            fixed(width: 150, height: 150) {
              ...GatsbyContentfulFixed_tracedSVG
            }
          }
        }
      }
    }
  }
`
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

错误信息:

TypeError: Cannot read property 'map' of null
Thumbs.renderItems
node_modules/react-responsive-carousel/lib/components/Thumbs.js:174
  171 | value: function renderItems() {
  172 |     var _this2 = this;
  173 | 
> 174 |     return this.state.images.map(function (img, index) {
  175 |         var itemClass = _cssClasses2.default.ITEM(false, index === _this2.state.selectedItem && _this2.state.hasMount);
  176 | 
  177 |         var thumbProps = {

最佳答案

您看到的错误来自 react-responsive-carousel 的缩略图组件.虽然该组件可以满足子 div 中的任何内容,但是,要使缩略图起作用,它需要一个 img 标签。由于使用 gatsby,您必须使用 <Img>组件,您可以通过禁用缩略图来解决此问题

<Carousel showThumbs={false} />

以下是一个完整的工作示例(使用 Typescript):

import * as React from "react";
import Img from "gatsby-image";
import { graphql, StaticQuery } from "gatsby";

import "react-responsive-carousel/lib/styles/carousel.min.css";
import { Carousel } from "react-responsive-carousel";


interface childImageSharpNode {
  childImageSharp: {
    fluid: any;
  };
}
interface SlideShowComponentProps {
  nodes: childImageSharpNode[];
}

class SlideShowComponent extends React.Component<SlideShowComponentProps> {
  constructor(props: SlideShowComponentProps) {
    super(props);
  }

  render() {
    const images: any[] = [];
    for (const node of this.props.nodes) {
      images.push(
        <div>
          <Img fluid={node.childImageSharp.fluid} alt="Image"></Img>
        </div>,
      );
    }

    return (
      <Carousel showThumbs={false} infiniteLoop={true} autoPlay={true}>
        {images}
      </Carousel>
    );
  }
}

interface StaticQueryProps {
  allFile: {
    nodes: childImageSharpNode[];
  };
}

const SlideShow: React.FC = () => (
  <StaticQuery
    query={graphql`
      query slideshow {
        allFile(filter: { relativePath: { regex: "/slideshow/" } }) {
          nodes {
            childImageSharp {
              fluid(maxWidth: 1024) {
                ...GatsbyImageSharpFluid
              }
            }
          }
        }
      }
    `}
    render={(data: StaticQueryProps) => <SlideShowComponent nodes={data.allFile.nodes}></SlideShowComponent>}
  />
);

export default SlideShow;

关于javascript - 如何使用 Contentful 在 Gatsby 中包含轮播组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55191747/

相关文章:

javascript - 将时间戳从 JSON 模型转换为可读格式

javascript - Bootstrap 菜单重叠下拉菜单的问题

graphql - 将多个查询的结果合并到一个数组中

javascript - 无法使用 jQuery 更改输入的 defaultValue 属性

javascript - 如何在一个函数完成后调用另一个函数?

html - 无法使 Bootstrap Sprite 工作

json - Gatsby:在页面上组合两个 graphql 源(.json 和 .jpg 源)

graphql - 电话号码的 Graphql 类型是什么

javascript - jQuery 防止提交输入发送表单

javascript - 通过mask获取所有元素