# useObjectUrl (/docs/hooks/use-object-url)





`useObjectUrl` turns a `Blob` or a `File` into a URL your page can load, and revokes it when the file changes or the component unmounts. Reach for it whenever you show a file the user picked or dropped. It's the one way the kit itself does it: [`FileItem`](/docs/file-item) and [`FileUpload`](/docs/file-upload) thumbnails, [`FilePreview`](/docs/file-preview), [`ImageCropper`](/docs/image-cropper) and [`PhotoCapture`](/docs/photo-capture) all go through it.

```tsx
import { useObjectUrl } from '@42-central/ui-react';

const url = useObjectUrl(file);

<img src={url} alt="" />
```

Never `URL.createObjectURL(file)` for an uploaded file. An object URL has your app's origin, so "Open image in new tab" on an uploaded `.svg` or `.html` runs its script as your app. `useObjectUrl` shows a file only under a type the kit allows:

| The source                                            | What you get                                                                    |
| ----------------------------------------------------- | ------------------------------------------------------------------------------- |
| PDF, raster image, common video or audio, plain text  | An object URL for the Blob re-wrapped under that type, never the type it claims |
| SVG, up to `SVG_DATA_URL_LIMIT` (5 MB)                | A `data:` URL, whose document has an opaque origin                              |
| Anything else: HTML, XML, an unknown or "binary" type | `undefined`, so nothing is shown                                                |
| A string                                              | Passed through untouched                                                        |

The full rules, and why, are in [Files › useObjectUrl](/docs/utilities/files#useobjecturl).

## Usage [#usage]

Pick any file, or try the two samples. The SVG comes back as a `data:` URL; the HTML file gets no URL at all.

<Preview code="&#x22;const [file, setFile] = useState<File>();\nconst url = useObjectUrl(file);\n\n<FileTrigger onSelect={([first]) => setFile(first)}>Choose a file</FileTrigger>\n{url ? <img src={url} alt=\&#x22;\&#x22; /> : <p>Nothing to show</p>}&#x22;">
  <ObjectUrlDemo />
</Preview>

The URL is made in an effect, never during render, so Strict Mode's double render leaks nothing. That makes it `undefined` on the server and for a Blob's first render (an SVG's until it's read): render a placeholder for that frame. After the source changes, the hook never hands out the previous, already revoked URL.

## API [#api]

<TypeTable
  type="{
  source: { type: 'Blob | string | null | undefined', required: true, description: 'A Blob or File to show, or a URL to pass through.' },
  'options.name': { type: 'string', description: &#x22;The file name, used when the type is missing or generic. A File's own name by default.&#x22; },
  'options.type': { type: 'string', description: &#x22;The MIME type, in place of the Blob's own.&#x22; },
}"
/>

Returns a `string`, or `undefined` while the URL is being made and for a Blob it won't show.
