Skip to Content
ohooks

useObjectUrl

Creates a temporary object URL (URL.createObjectURL) for a Blob, File, or MediaSource and automatically revokes it on change or unmount.

Loading preview...

The useObjectUrl hook generates a blob: URL via URL.createObjectURL whenever a Blob, File, or MediaSource is provided and automatically calls URL.revokeObjectURL when the input changes or the component unmounts to prevent memory leaks.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useObjectUrl.json

Usage

Examples

Instant Image Upload Preview

Pass a selected File from an <input type="file"> to preview the image before uploading it to a server.

Downloadable Dynamically Generated Blob

Create a Blob in memory and pass it to useObjectUrl to power a client-side download link.