T
TanStack3y ago
xenial-black

<video> tag flashes when using react query

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,
})
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?
12 Replies
xenial-black
xenial-blackOP3y ago
Gist
editor.tsx
GitHub Gist: instantly share code, notes, and snippets.
xenial-black
xenial-blackOP3y ago
fascinating-indigo
fascinating-indigo3y ago
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
xenial-black
xenial-blackOP3y ago
that's why I'm confused
xenial-black
xenial-blackOP3y ago
No description
No description
xenial-black
xenial-blackOP3y ago
i have keys on everything that's mapped
fascinating-indigo
fascinating-indigo3y ago
If you've got the react devtools browser extension you can probably see what's causing a re-render
xenial-black
xenial-blackOP3y ago
is there a way to see what causes renders?
fascinating-indigo
fascinating-indigo3y ago
using the extension
xenial-black
xenial-blackOP3y ago
I have it
constant-blue
constant-blue3y ago
react-query refetches on the client with default staleTime of zero
xenial-black
xenial-blackOP3y ago
that makes sense, I ended up setting it to Infinity for now, although I would really like to find a way to have react query refetch and update the <video> tags without having that flash of content, but that's not a tanstack issue

Did you find this page helpful?