简体   繁体   English

如何将照片注入 Gatsby 网站?

[英]How to inject photos to a Gatsby site?

I have a Gatsby and Strapi photo blog and I want to have a home page that loads 10 pictures at a time until the user hits the bottom of the page, then load the next ten etc, so that the user isn't downloading all photos at once.我有一个 Gatsby 和 Strapi 照片博客,我想要一个主页,一次加载 10 张图片,直到用户点击页面底部,然后加载接下来的十张等,这样用户就不会下载所有照片立刻。

I'm using useStaticQuery to load the images initially.我最初使用useStaticQuery加载图像。 However, that can only run at build time.但是,这只能在构建时运行。 Is there a way to make another graphQL call when the user hits the bottom of the page and add it to my state?当用户点击页面底部并将其添加到我的 state 时,有没有办法进行另一个 graphQL 调用? Or is this the "static" part of a static site generator.或者这是 static 站点生成器的“静态”部分。

Alternatively, does making the graphQL call for all photo data make its way to the client device if I don't render it?或者,如果我不渲染它,是否让所有照片数据的 graphQL 调用进入客户端设备? Say if I just use React to render parts of the array at a time?假设我一次只使用 React 渲染数组的一部分?

Below is my home page.下面是我的主页。 I'm using Karl Run's bottom scroll listener , and the Photos component renders the photos as a list.我正在使用Karl Run 的底部滚动侦听器,并且 Photos 组件将照片呈现为列表。

const IndexLayout: React.FC<Props> = () => {
  const [photosData, setPhotosData] = useState<InitialQueryType['allStrapiPhoto']>(getSiteMetaDataAndTenPhotos().allStrapiPhoto)

  const handleOnDocumentBottom = useCallback(() => {
    console.log('at bottom, make a call')

    // this throws an invalid hook error as getTenPhotos calls useStaticQuery
    let morePhotosData = getTenPhotos(photosData.edges.length)

    setPhotosData({ ...photosData, ...morePhotosData })
  }, [photosData])

 useBottomScrollListener(handleOnDocumentBottom)

  return (
    <LayoutRoot>
      <div className={styles.bigHomeContainer}>
        <div className={styles.titleContainer}>
          <h1 className={styles.indexHeading}>TayloredToTaylor's Wildlife Photos</h1>
        </div>
        <Photos photos={photosData} />
      </div>
    </LayoutRoot>
  )
}

export default IndexLayout

Github Repo Github 回购

As you said, queries are called in the build-time.正如您所说,查询是在构建时调用的。 However, one workaround that may work for you is to retrieve all photos at the beginning (build-time) and show them on-demand in groups of 10 triggered by time or by the user's scroll, etc. Adapting something like this to your use-case should work:但是,一种可能对您有用的解决方法是在开始时(构建时)检索所有照片,并按时间或用户滚动等触发的 10 组按需显示它们。根据您的使用调整类似这样的内容- 案例应该工作:

Fetch all photos with:获取所有照片:

  const allPhotos=<InitialQueryType['allStrapiPhoto']>(getSiteMetaDataAndTenPhotos().allStrapiPhoto)

Set the current currentIndex :设置当前currentIndex

const [currentIndex, setCurrentIndex]= useState(0);

And ask them on-demand:并按需询问他们:

const morePhotosData = ()=>{
   let cloneOfAllPhotos= [...allPhotos];
   let newPhotos= cloneOfAllPhotos.splice(currentIndex, currentIndex + 10) // change 10 to your desired value
   
   setPhotosData(newPhotos);
   setCurrentIndex(currentIndex+10);
}

Basically, you are cloning the allPhotos ( let cloneOfAllPhotos= [...allPhotos] ) to manipulate that copy and splicing them with a dynamic index ( cloneOfAllPhotos.splice(currentIndex,currentIndex + 10) ) that is increasing by 10 in each trigger of morePhotosData .基本上,您正在克隆allPhotos ( let cloneOfAllPhotos= [...allPhotos] ) 来操作该副本并使用动态索引 ( cloneOfAllPhotos.splice(currentIndex,currentIndex + 10) ) 拼接它们,该索引在每次触发时增加 10 morePhotosData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM