# Images and other Media

Reactive Data Client can also fetch media, so images and other binary data get Suspense and
[render-as-you-fetch](https://dataclient.io/docs/guides/render-as-you-fetch.md).

## Storing ArrayBuffer

Do not use [Resource](https://dataclient.io/rest/api/resource.md) or [Entity](https://dataclient.io/rest/api/Entity.md) for binary data. They model objects with
keys. Define an [Endpoint](https://dataclient.io/rest/api/Endpoint.md) instead.

```typescript
import { Endpoint } from '@data-client/rest';

export const getPhoto = new Endpoint(async ({ userId }: { userId: string }) => {
  const response = await fetch(`/users/${userId}/photo`);
  const photoArrayBuffer = await response.arrayBuffer();

  return photoArrayBuffer;
});
```

**useSuspense**

```tsx
// photo is typed as ArrayBuffer
const photo = useSuspense(getPhoto, { userId });
```

**useCache**

```tsx
// photo will be undefined if the fetch hasn't completed
// photo will be ArrayBuffer if the fetch has completed
const photo = useCache(getPhoto, { userId });
```

**JS/Node**

```tsx
// photo is typed as ArrayBuffer
const photo = await getPhoto({ userId });
```

## Just Images

Images can suspend as well, so a Suspense boundary waits for them to load. This works best with
[render-as-you-fetch](https://dataclient.io/docs/guides/render-as-you-fetch.md).

[@data-client/img](https://www.npmjs.com/package/@data-client/img) has an `<Img />` component that suspends, and a
`getImage` endpoint to prefetch.

## Installation

```bash
npm install @data-client/img
```

## Usage

```tsx title="Profile.tsx"
import React from 'react';
import { useSuspense } from '@data-client/react';
import { Img } from '@data-client/img';
import { UserResource } from './resources/User';

export default function Profile({ username }: { username: string }) {
  const user = useSuspense(UserResource.get, { username });
  return (
    <div>
      <Img
        src={user.img}
        alt="React Logo"
        style={{ height: '32px', width: '32px' }}
      />
      <h2>{user.fullName}</h2>
    </div>
  );
}
```

#### Prefetching

The example above waits for the user before the image request starts. If you can compute the image URL from the same
parameters, start both requests at the same time:

```tsx title="Profile.tsx"
import React from 'react';
import { useSuspense, useFetch } from '@data-client/react';
import { Img, getImage } from '@data-client/img';
import { UserResource } from './resources/User';

export default function Profile({ username }: { username: string }) {
  const imageSrc = `/profile_images/${username}`;
  useFetch(getImage, { src: imageSrc });
  const user = useSuspense(UserResource.get, { username });
  return (
    <div>
      <Img
        src={imageSrc}
        alt="React Logo"
        style={{ height: '32px', width: '32px' }}
      />
      <h2>{user.fullName}</h2>
    </div>
  );
}
```

With [render-as-you-fetch](https://dataclient.io/docs/guides/render-as-you-fetch.md), call [Controller.fetch()](https://dataclient.io/docs/api/Controller.md#fetch) with
the `getImage` [Endpoint](https://dataclient.io/rest/api/Endpoint.md) to preload the image.
