#<video> tag flashes when using react query

13 messages · Page 1 of 1 (latest)

charred pebble
#

Hi all, I'm using react query to manage my browser based video editor's asset library:

const assetQuery = useQuery({
    queryKey: ["assets"],
    queryFn: async () => {
      const response = await fetch("/api/assets")
      const data = await response.json()

      const assetsPresignedURLs = await Promise.all(
        data.map((asset: Asset) =>
          fetch(`/api/s3/presigned-get/`, {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify({ key: asset.key }),
          })
            .then((response) => response.json())
            .then((data) => data.url)
        )
      )

      data.forEach((asset: Asset & { presignedURL: string }, index: number) => {
        asset.presignedURL = assetsPresignedURLs[index]
      })

      return data
    },
    initialData: editorAssets,
  })

Since the files are hosted in a private Cloudflare R2 bucket, I need to get presigned URLs to access them so I set up this query to do so. However whenever the page loads, it shows the initial content (thanks to SSR from NextJS) and then flashes once react query revalidates.

I checked both JSON objects, they're the exact same. Why would it cause a re-render?

charred pebble
vagrant gate
# charred pebble

make sure that when you map over your assets that they have a key, this isn't a react-query issue but an issue with how react handles re-renders

#

and mapping over items

charred pebble
#

i have keys on everything that's mapped

vagrant gate
#

If you've got the react devtools browser extension you can probably see what's causing a re-render

charred pebble
vagrant gate
charred pebble
#

I have it

dreamy halo
#

react-query refetches on the client with default staleTime of zero