【问题标题】:Gatsby-image-background-slider triggers TypeError: Cannot read property 'style' of undefinedGatsby-image-background-slider 触发 TypeError: Cannot read property 'style' of undefined
【发布时间】:2021-01-12 02:05:04
【问题描述】:

我在我的项目中使用gatsby-image-background-slider,它会触发TypeError: Cannot read property 'style' of undefined changeIndex src/index.js:111 callback src/index.js:129

最初是同一个英雄,但在开发过程中,它开始出现此错误。我已经重新安装了软件包,尝试使用相同代码的新 hello-world-project。

    Hero.js

    import React from "react"
    import { useStaticQuery, graphql } from "gatsby"
    import BackgroundSlider from 'gatsby-image-background-slider'
    
    const Hero = ({ children }) => (
      <>
        <main>{children}</main>
        <BackgroundSlider
          query={useStaticQuery(graphql`
            query {
              backgrounds: allFile (filter: {sourceInstanceName: {eq: "backgrounds"}}){
                nodes {
                  relativePath
                  childImageSharp {
                    fluid (maxWidth: 4000, quality: 100){
                      ...GatsbyImageSharpFluid
                    }
                  }
                }
              }
            }
          `)}
          initDelay={2}
          transition={4}
          duration={8}
          images={["picture2.jpg", "picture2.jpg"]}
        >
        </BackgroundSlider>
      </>
    )
    
    export default Hero

你知道是什么原因造成的吗?

【问题讨论】:

  • prop的风格在哪里?
  • 我使用全局 css。

标签: reactjs gatsby


【解决方案1】:

As it's inferred from the documentation(以及其他示例),似乎添加initDelaytransitiondurationprops 会强制组件也接收styleprops。由于您没有传递它,它会破坏依赖代码。

如果您不打算添加任何 style 属性,您可以简单地使用虚拟样式,例如:

    <BackgroundSlider
      query={useStaticQuery(graphql`
        query {
          backgrounds: allFile (filter: {sourceInstanceName: {eq: "backgrounds"}}){
            nodes {
              relativePath
              childImageSharp {
                fluid (maxWidth: 4000, quality: 100){
                  ...GatsbyImageSharpFluid
                }
              }
            }
          }
        }
      `)}
      initDelay={2}
      transition={4}
      duration={8}
      style={{`display: initial`}}
      images={["picture2.jpg", "picture2.jpg"]}
    >
    </BackgroundSlider>

由于该插件维护程度低且没有提供更好的示例,我建议在gatsby-background-image 中使用自定义&lt;div&gt; 结构以及另一个轮播依赖项,例如React Slick Slider。比如:

const SimpleSlider=()=> {
  const data = useStaticQuery(graphql`
      query {
          backgrounds: allFile (filter: {sourceInstanceName: {eq: "backgrounds"}}){
              nodes {
                  relativePath
                  childImageSharp {
                      fluid (maxWidth: 4000, quality: 100){
                          ...GatsbyImageSharpFluid
                      }
                  }
              }
          }
      }
  `)

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

  return <div>
      <h2> Single Item</h2>
      <Slider {...settings}>
        { backgrounds.nodes.map(background =>{
            return <BackgroundImage
              Tag="section"
              className={`yourClassName`}
              fluid={background.childImageSharp.fluid}
              backgroundColor={`#040e18`}
              style={{`height:100%; width:100%`}}
            />
          })}
      </Slider>
    </div>
}

请注意,如果没有沙箱,您可能需要调整一点 sn-p,尤其是使用 styles prop,但您明白了。

【讨论】:

  • 感谢您的回答!由于某种原因,添加样式道具不能解决问题。很奇怪。
  • 可以升级包版本吗?另外,如果你删除了initDelaytransitiondurationstyle 道具,会发生什么?会一直断吗?
  • 遗憾的是,既没有解决也没有改变错误。
  • 你用的是哪个版本?
  • "gatsby": "^2.26.1", "gatsby-background-image": "^1.3.1", "gatsby-image": "^2.9.0", "gatsby-image-background-slider": "0.0.4",
猜你喜欢
  • 2021-04-09
  • 1970-01-01
  • 2021-06-25
  • 2017-04-24
  • 2023-03-15
  • 2020-09-20
  • 2021-09-18
  • 2021-09-25
  • 1970-01-01
相关资源
最近更新 更多