Skip to content

Commit 1688e56

Browse files
BlurrableImage state initialization (#671)
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent c15cd90 commit 1688e56

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

app/components/blurrable-image.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ function BlurrableImage({
1515
const [visible, setVisible] = React.useState(() => {
1616
if (isServer) return false
1717

18+
// During hydration the element might not be in the DOM yet, so guard
19+
// against null to avoid crashing and fall back to the blurred state.
20+
const el = document.getElementById(id)
21+
if (!(el instanceof HTMLImageElement)) return false
22+
1823
// on the client, it's possible the images has already finished loading.
1924
// we've got the data-evt-onload attribute on the image
2025
// (which our entry.server replaces with simply "onload") which will remove
2126
// the class "opacity-0" from the image once it's loaded. So we'll check
2227
// if the image is already loaded and if so, we know that visible should
2328
// initialize to true.
24-
const el = document.getElementById(id)
25-
return el instanceof HTMLImageElement && !el.classList.contains('opacity-0')
29+
return !el.classList.contains('opacity-0')
2630
})
2731
const jsImgElRef = React.useRef<HTMLImageElement>(null)
2832

0 commit comments

Comments
 (0)