Skip to content

Commit

Permalink
fix(use-image): set status after hydrated (#4486)
Browse files Browse the repository at this point in the history
* fix(use-image): set status after hydrated

* fix(use-image): use useSafeLayoutEffect instead
  • Loading branch information
wingkwong authored Jan 3, 2025
1 parent 0eb96f6 commit 5d0fc9f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-pigs-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/use-image": patch
---

set status after hydrated
11 changes: 8 additions & 3 deletions packages/hooks/use-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {ImgHTMLAttributes, SyntheticEvent} from "react";

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

type NativeImageProps = ImgHTMLAttributes<HTMLImageElement>;

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

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

const [status, setStatus] = useState<Status>(() =>
isHydrated ? setImageAndGetInitialStatus(props, imageRef) : "pending",
);
const [status, setStatus] = useState<Status>("pending");

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

useSafeLayoutEffect(() => {
if (isHydrated) {
setStatus(setImageAndGetInitialStatus(props, imageRef));
}
}, [isHydrated]);

/**
* If user opts out of the fallback/placeholder
* logic, let's just return 'loaded'
Expand Down

0 comments on commit 5d0fc9f

Please sign in to comment.