useImageLoader is a hook for loading an image with convenient state flags: isPending, isSuccess, isError, and error. It returns a hybrid structure (tuple + object) whose fields are computed on demand.
The hook starts loading on every imageUrl change, reports success/error/abort, and in development warns if an empty string is provided.
function useImageLoader(imageUrl: string): UseImageReturn;-
Parameters
imageUrl— the URL of the image to load.
-
Returns:
UseImageReturn— a hybrid structure with loading statuses:isPending: booleanisSuccess: booleanisError: booleanerror: { message: string } | null
import { useImageLoader } from '@webeach/react-hooks/useImageLoader';
type AvatarProps = {
src: string;
alt: string;
};
export function Avatar(props: AvatarProps) {
const { src, alt } = props;
const status = useImageLoader(src);
if (status.isPending) {
return <Spinner />;
}
if (status.isError) {
return <FallbackAvatar alt={alt} />;
}
return <img src={src} alt={alt} />;
}import { useImageLoader } from '@webeach/react-hooks/useImageLoader';
type GalleryItemProps = {
image: string;
}
export function GalleryItem(props: GalleryItemProps) {
const { image } = props;
const [isPending, isSuccess, isError, error] = useImageLoader(image);
return (
<figure>
{isSuccess && <img src={src} alt="" />}
{isPending && <Skeleton width={160} height={120} />}
{isError && <div className="error">{error?.message}</div>}
</figure>
);
}-
Loading triggers
- On the initial mount and on every
imageUrlchange, the hook starts a new load and setsisPending.
- On the initial mount and on every
-
Status transitions
- Success —
isSuccess = trueafteronload. - Error —
isError = trueanderroris filled afteronerror. - Abort — treated as an error with an "aborted" message (via
onabort).
- Success —
-
Cleanup
- On unmount, handlers are removed to avoid leaks. The browser network request is not forcibly aborted.
-
Dev warning
- In development, when
imageUrl === '', a warning is logged: an empty URL may trigger a request to the current page as an image.
- In development, when
-
SSR safety
- The logic uses
useEffect, so it does not run on the server.
- The logic uses
- Delaying content display until the image is ready (skeleton/loader/fallback).
- Preloading gallery/card images before rendering.
- Simple status indication without manually handling
Imageand event handlers.
- If you need to obtain the
HTMLImageElementitself or finely control loading/cancellation — write your own wrapper aroundImageor use Fetch +AbortController. - If the image is already guaranteed to be in cache and no status is needed — you can render
<img>directly.
-
Empty URL
- Passing
''leads to a dev warning and may cause an incorrect request. Pass a valid URL.
- Passing
-
Expecting automatic cancellation
- The hook removes handlers but does not abort the browser's network request. For hard cancellation, use a different approach (e.g., preload via Fetch with
AbortController).
- The hook removes handlers but does not abort the browser's network request. For hard cancellation, use a different approach (e.g., preload via Fetch with
-
Incorrect display logic
- Don’t forget to check
isErrorand show a fallback;isSuccesswon’t occur on error/abort.
- Don’t forget to check
Exported types
UseImageReturn- Hybrid: tuple
[isPending, isSuccess, isError, error]and object{ isPending; isSuccess; isError; error }.
- Hybrid: tuple