Skip to content
SnapDOMTry SnapStudioGitHub★ 8K
How-To Recipe

Export a React component as an image

Download a rendered React card as a PNG from a click handler. Keep the export controls outside the card; use the React guide for previews and reusable hooks.

TL;DR

Keep a ref on the card, capture it with snapdom(ref.current), then call download({ format: 'png' }).

Capture the component through a ref

Install @zumer/snapdom. The button below downloads only the article; it stays outside the captured root:

import { useRef } from 'react'
import { snapdom } from '@zumer/snapdom'

export function ShareCard() {
  const cardRef = useRef(null)
  const save = async () => {
    const capture = await snapdom(cardRef.current, { embedFonts: true })
    await capture.download({ format: 'png', filename: 'share-card' })
  }
  return <>
    <article ref={cardRef}>Your component</article>
    <button onClick={save}>Download PNG</button>
  </>
}

React renders, SnapDOM captures the DOM

SnapDOM is framework-agnostic: React renders the component and SnapDOM captures the resulting DOM. The ref stays scoped to this component instance. For an in-app preview, object URL cleanup or a reusable hook, follow the React integration guide.

Edit and capture component state

Change the text directly, then capture the rendered state a React ref would point to.

EDIT ME

React component state

Click this text, change it, then capture the current DOM.

The captured result will appear here.

Limits and common mistakes

Call capture in the browser, not during server rendering. Wait for Suspense data, images and fonts if they materially affect the component.

Frequently asked questions

Does this work with React Server Components?

The component may originate on the server, but capture must run from a client component after a real DOM element exists.

Can I export SVG instead?

Yes. Replace download format png with svg, or call snapdom.toSvg(ref.current).