feat: add object URL hook for local file previews

This commit is contained in:
rajun 2026-10-27 16:28:53 +05:30
parent bf3ba351d4
commit 8034b851d0

View File

@ -0,0 +1,18 @@
import { useEffect, useState } from 'react';
// A temporary URL for previewing a local file; revoked when the file changes or the component unmounts
export function useObjectUrl(file: File | null) {
const [url, setUrl] = useState<string>();
useEffect(() => {
if (!file) {
setUrl(undefined);
return;
}
const objectUrl = URL.createObjectURL(file);
setUrl(objectUrl);
return () => URL.revokeObjectURL(objectUrl);
}, [file]);
return url;
}