【问题标题】:Image change using setInterval in React/Gatsby with Graphql and Gatsby Image使用 Graphql 和 Gatsby Image 在 React/Gatsby 中使用 setInterval 更改图像
【发布时间】:2021-03-29 01:45:13
【问题描述】:

我正在尝试根据 Graphql 查询的结果在 Gatsby/React 中创建一个图像更改组件。 我被卡住了,不知道在 setInterval 函数中该做什么。任何帮助表示赞赏。谢谢

import React, { useState, useEffect } from "react";
import { graphql, useStaticQuery } from "gatsby";
import Img from "gatsby-image";

function ImgRotator() {
   const imageData = useStaticQuery(graphql`
      query {
         allFile(filter: { extension: { regex: "/(jpg)|(jpeg)|(png)/" }, relativeDirectory: { eq: "rotator" } }) {
            edges {
               node {
                  id
                  childImageSharp {
                     fluid(maxWidth: 2880) {
                        ...GatsbyImageSharpFluid_withWebp
                     }
                  }
               }
            }
         }
      }
   `);

   const allImages = imageData.allFile.edges;
   const [images] = useState(allImages);
   const [displayImage, setDisplayImage] = useState(imageData.allFile.edges[0].node.childImageSharp.fluid);


   useEffect(() => {
      const interval = setInterval(() => {

         // Help! Don't know what to do here

      }, 1000);
      return () => clearInterval(interval);
   }, [displayImage]);

   return <Img fluid={displayImage} alt="" loading="eager" />;
}

export default ImgRotator; 
 

【问题讨论】:

  • 不难想象你需要一个计数器/索引(状态),并且应该在间隔内增加,重置为最大值......基本反应状态动作......自动增加计数器示例

标签: javascript reactjs graphql gatsby gatsby-image


【解决方案1】:

一种可能适合您的解决方法是:

    import React, { useState, useEffect } from "react";
    import { graphql, useStaticQuery } from "gatsby";
    import Img from "gatsby-image";
    
    function ImgRotator() {
       const imageData = useStaticQuery(graphql`
          query {
             allFile(filter: { extension: { regex: "/(jpg)|(jpeg)|(png)/" }, relativeDirectory: { eq: "rotator" } }) {
                edges {
                   node {
                      id
                      childImageSharp {
                         fluid(maxWidth: 2880) {
                            ...GatsbyImageSharpFluid_withWebp
                         }
                      }
                   }
                }
             }
          }
       `);
    
       const allImages = imageData.allFile.edges;
       const [currentImage, setCurrentImage]=useState({});
       const [currentIndex, setCurrentIndex]=useState(0);
    
    
       useEffect(() => {
          const interval = setInterval(() => {
                 
            setCurrentImage(allImages[currentIndex].node.childImageSharp.fluid)
             setCurrentIndex(currentIndex++)
                  
          }, 1000);
          return () => clearInterval(interval);
       }, [currentIndex]);
    
       return <Img fluid={currentImage} alt="" loading="eager" />;
    }
    
    export default ImgRotator; 

总结一下,你最初设置了两个状态:

   const [currentImage, setCurrentImage]=useState({});
   const [currentIndex, setCurrentIndex]=useState(0);

不言自明:currentImage 将存储您显示的图像,currentIndex 将存储该图像数组中的索引。

在您的useEffect(一旦currentIndex 更改时触发)中,您定义了一个区间,它在allImages 中查找currentIndex 并使用该索引设置currentImage。每秒都会再次触发该功能,因此它将更新系统。

您的gatsby-image 将根据该间隔显示currentImage

   return <Img fluid={currentImage} alt="" loading="eager" />;

【讨论】:

  • 非常感谢费兰。您的解决方案有效。我的设置中只有一个问题:图像在更改之间闪烁,就像它们被卸载和重新安装一样有没有办法让图像之间没有间隙的平滑更改?
  • @FerranBuireu 不在“纯反应”中...旧节点刚刚被删除,没有可能的淡出/滑动... css 只能影响出现的元素...您必须使用一些反应动画库...或尝试使用键渲染 2 个(上一个+当前)图像
猜你喜欢
  • 2020-05-10
  • 2020-03-09
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2020-07-13
  • 2021-03-27
  • 1970-01-01
  • 2021-03-18
相关资源
最近更新 更多