首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Gatsby中包含Contentful中的carousel组件?

如何在Gatsby中包含Contentful中的carousel组件?
EN

Stack Overflow用户
提问于 2019-03-16 07:06:24
回答 1查看 2.2K关注 0票数 4

我想在我的Gatsby项目中包含一个图像传送带。所有图像都将从Contentful中查询。

我使用了“react-responsive carousel”,并设法让它直接与简单的导入图像一起工作。我还确信,我可以在不将其放入carousel组件的情况下成功地拉入内容图像。

有谁能帮我一下吗?似乎gatsby-image组件Img没有返回"react-reponsive-carousel“可以识别的标记。或者你知道其他可以工作的旋转木马吗?

我的代码:

代码语言:javascript
复制
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
            }
          }
        }
      }
    }
  }
`
代码语言:javascript
复制
<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>

错误消息:

代码语言:javascript
复制
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 = {

EN

回答 1

Stack Overflow用户

发布于 2019-07-26 22:16:23

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

<Carousel showThumbs={false} />

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

代码语言:javascript
复制
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;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55191747

复制
相关文章

相似问题

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