---
url: https://paperpic.dev/reference/react.md
description: >-
  How the PaperPic component for React maps the options, events and methods of
  the core to props, callback props and a ref.
---

# React

`@paperpic/react` wraps the viewer in a component for React 19 or newer. The options are props, the events are callback props, and the methods are on the viewer that `ref` receives.

```tsx
import type { ExportedImage } from '@paperpic/react'
import { PaperPic } from '@paperpic/react'
import '@paperpic/react/style.css'

function upload({ blob }: ExportedImage) {
  // Send the edited image to your server
}

export function Receipt() {
  return (
    <div style={{ height: 480 }}>
      <PaperPic src="/receipt.jpg" alt="A receipt" onApply={upload} />
    </div>
  )
}
```

The component fills its parent element, so give the parent a height.

## Props

Every [option](./options) is a prop with the same name, such as `editable={false}` or `aspectRatio={210 / 297}`. Two more props style the element that holds the viewer:

| Prop        | Type            | Description                       |
| ----------- | --------------- | --------------------------------- |
| `className` | `string`        | Classes on top of `paperpic-host` |
| `style`     | `CSSProperties` | Inline styles                     |

An object or array with the same contents as before, such as `tools={['crop', 'rotate']}`, changes nothing, so inline values are fine. A function is new on every render, so define the functions of `labels`, such as `zoomLevel`, outside the component, or build those labels with `useMemo`.

## Events

| Prop         | Payload                                        |
| ------------ | ---------------------------------------------- |
| `onApply`    | [`ExportedImage`](./types#exportedimage)       |
| `onChange`   | [`PaperPicState`](./state)                     |
| `onDownload` | [`PaperPicDownload`](./types#paperpicdownload) |
| `onError`    | `{ error, phase }`                             |
| `onLoad`     | [`Size`](./types#size)                         |

[Events](./events) describes each one. A new function does not recreate the viewer, so inline functions are fine. A change of props never calls `onChange`.

`onChange` receives the [state](./state) of the viewer, not a DOM event. [State](./state#in-step) shows how to keep it with `useState`, `onChange`, `onLoad` and `onError`.

## Methods {#methods}

`ref` receives the viewer, of type [`PaperPicInstance`](./types#paperpicinstance), with the [methods](./methods), such as `zoomIn()` or `apply()`. It is `null` until the component mounts:

```tsx
import type { PaperPicInstance } from '@paperpic/react'
import { PaperPic } from '@paperpic/react'
import { useRef } from 'react'

export function Viewer({ src }: { src: string }) {
  const picRef = useRef<PaperPicInstance>(null)

  return (
    <>
      <button type="button" onClick={() => picRef.current?.zoomIn()}>Zoom in</button>
      <div style={{ height: 480 }}>
        <PaperPic ref={picRef} src={src} toolbar={false} />
      </div>
    </>
  )
}
```

The component creates the viewer when it mounts and destroys it when it unmounts, so do not call `destroy()`. Change the props instead of calling `setOptions()`, which the next change of a prop would undo, and use the callback props instead of `on()`.

Passing the same `src` again does not reload the image, so it keeps the result of an edit. To bring back the original, call `picRef.current.reload()`.

## Types {#types}

`@paperpic/react` exports every type of `@paperpic/core`, so you do not need to install it: [Types](./types) shows each one. It also exports the types of the component: [`PaperPicProps`](./types#paperpicprops), with its props, and [`PaperPicInstance`](./types#paperpicinstance), the viewer that `ref` receives.

## Server-side rendering

The component is marked with `'use client'`. With server-side rendering, such as in Next.js, it renders an empty element on the server and creates the viewer in the browser.
