Skip to content

Commit 5d0fc9f

Browse files
authored
fix(use-image): set status after hydrated (#4486)
* fix(use-image): set status after hydrated * fix(use-image): use useSafeLayoutEffect instead
1 parent 0eb96f6 commit 5d0fc9f

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

.changeset/fair-pigs-tap.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nextui-org/use-image": patch
3+
---
4+
5+
set status after hydrated

packages/hooks/use-image/src/index.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {ImgHTMLAttributes, SyntheticEvent} from "react";
66

77
import {useRef, useState, useEffect, MutableRefObject} from "react";
88
import {useIsHydrated} from "@nextui-org/react-utils";
9+
import {useSafeLayoutEffect} from "@nextui-org/use-safe-layout-effect";
910

1011
type NativeImageProps = ImgHTMLAttributes<HTMLImageElement>;
1112

@@ -71,9 +72,7 @@ export function useImage(props: UseImageProps = {}) {
7172

7273
const imageRef = useRef<HTMLImageElement | null>(isHydrated ? new Image() : null);
7374

74-
const [status, setStatus] = useState<Status>(() =>
75-
isHydrated ? setImageAndGetInitialStatus(props, imageRef) : "pending",
76-
);
75+
const [status, setStatus] = useState<Status>("pending");
7776

7877
useEffect(() => {
7978
if (!imageRef.current) return;
@@ -97,6 +96,12 @@ export function useImage(props: UseImageProps = {}) {
9796
}
9897
};
9998

99+
useSafeLayoutEffect(() => {
100+
if (isHydrated) {
101+
setStatus(setImageAndGetInitialStatus(props, imageRef));
102+
}
103+
}, [isHydrated]);
104+
100105
/**
101106
* If user opts out of the fallback/placeholder
102107
* logic, let's just return 'loaded'

0 commit comments

Comments
 (0)