feat: add object URL hook for local file previews
This commit is contained in:
parent
bf3ba351d4
commit
8034b851d0
18
app/hooks/use-object-url.ts
Normal file
18
app/hooks/use-object-url.ts
Normal 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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user