Skip to content

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 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:

PropTypeDescription
classNamestringClasses on top of paperpic-host
styleCSSPropertiesInline 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 ​

PropPayload
onApplyExportedImage
onChangePaperPicState
onDownloadPaperPicDownload
onError{ error, phase }
onLoadSize

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 of the viewer, not a DOM event. State shows how to keep it with useState, onChange, onLoad and onError.

Methods ​

ref receives the viewer, of type PaperPicInstance, with the 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 ​

@paperpic/react exports every type of @paperpic/core, so you do not need to install it: Types shows each one. It also exports the types of the component: PaperPicProps, with its props, and 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.

Released under the MIT License.