首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将react-bootstrap carousel与gatsby-image一起使用?

如何将react-bootstrap carousel与gatsby-image一起使用?
EN

Stack Overflow用户
提问于 2019-06-04 03:36:00
回答 1查看 2.7K关注 0票数 1

我正在尝试使用gatsby-image构建一个react-bootstrap carousel,因为使用标准img标记的加载速度很慢。然而,我面临的问题是,我想为文件夹中的所有图像编写一个组件,而不是像gatsby-starter-default中那样为每个文件编写一个组件。因此,我希望Image组件返回一个图像数组,以便在<Image[0] />之类的Carousel中使用。或者将relativePath传递给旋转木马内部的组件,如<Image(carouselImages/carousel_1.jpeg) />

请看下面的carousel.js代码(为了可读性,减少到2张幻灯片)。

代码语言:javascript
复制
import React from 'react'
import Carousel from 'react-bootstrap/Carousel'
import Button from 'react-bootstrap/Button'
import './style.scss'
import { injectIntl, Link, FormattedMessage } from "gatsby-plugin-intl"

import Image1 from 'components/img1'
import Image2 from 'components/img2'

class ControlledCarousel extends React.Component {
  constructor(props, context) {
    super(props, context)

    this.handleSelect = this.handleSelect.bind(this)

    this.state = {
      index: 0,
      direction: null,
    }
  }

  handleSelect(selectedIndex, e) {
    this.setState({
      index: selectedIndex,
      direction: e.direction,
    })
  }

  render() {
    const { index, direction } = this.state

    return (
      <Carousel
        activeIndex={index}
        direction={direction}
        onSelect={this.handleSelect}>

        <Carousel.Item>
          <div className="imgWrapper">
           <Image1 />
          </div>
          <Carousel.Caption>
            <p> some text </p>
          </Carousel.Caption>
        </Carousel.Item>

        <Carousel.Item>
          <div className="imgWrapper">
           <Image2 />
          </div>
          <Carousel.Caption>
            <p> some text </p>
          </Carousel.Caption>
        </Carousel.Item>
      </Carousel>
    )
  }
}
export default ControlledCarousel

以及Image组件本身:

代码语言:javascript
复制
import React from "react"
import { StaticQuery, graphql } from "gatsby"
import Img from "gatsby-image"

const Image = () => (
  <StaticQuery
    query={graphql`
      query {
        placeholderImage: file(relativePath: { eq: "carouselImages/carousel_1.jpg" }) {
          childImageSharp {
            fluid(maxWidth: 2500) {
              ...GatsbyImageSharpFluid
            }
          }
        }
      }
    `}
    render={data => <Img fluid={data.placeholderImage.childImageSharp.fluid} />}
  />
)
export default Image

对于如何让它们协同工作,有什么建议吗?非常感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2019-08-06 14:06:08

打开src/images中的一个文件夹,并将所有图片放在其中,然后使用StaticQuery中的过滤器来搜索该文件夹中的所有图片。

代码语言:javascript
复制
import React from 'react';
import { StaticQuery, graphql } from 'gatsby';
import Img from 'gatsby-image';
import Carousel from 'react-bootstrap/Carousel';

export default () => (
 <StaticQuery
    query={graphql`
    query {
      allFile(filter: {relativeDirectory: {eq: "carousel"}}) {
        edges {
          node {
            childImageSharp {
              fluid(quality: 90) {
                ...GatsbyImageSharpFluid
              }
            }
          }
        }
      }
    }
  `} 
  render={data=> (
      <Carousel className="full-width-md d-none d-md-block">
      {data.allFile.edges.map(pic => 
      <Carousel.Item>
          <Img fluid={pic.node.childImageSharp.fluid}/>
      </Carousel.Item>
      )}
  </Carousel>
  )}
    />
);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56433537

复制
相关文章

相似问题

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