I'm trying to create an Image changing component in Gatsby/React from the results on a Graphql query. I am stuck and don't know what to do inside that setInterval function. Any Help appreciated. Thanks
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;