React component state
Click this text, change it, then capture the current DOM.
Attach a ref to the rendered component and capture the real browser DOM in a click handler.
Use a React ref, then call snapdom.toPng(ref.current) after the component has rendered.
Install @zumer/snapdom, select the rendered element, and capture it:
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>
</>
}SnapDOM is framework-agnostic: React renders the component and SnapDOM captures the resulting DOM. A ref is more reliable than a global selector and stays scoped to the component instance.
Change the text directly, then capture the rendered state a React ref would point to.
Click this text, change it, then capture the current DOM.
For the ref pattern above, call capture after hydration rather than during server rendering. Wait for Suspense data, images and fonts if they materially affect the component. A backend capture still needs a real browser page: open the rendered route with Playwright or Puppeteer, then inject and run SnapDOM there.
Yes. For an in-app export, put the ref and capture handler in a Client Component and run it after hydration. For server-generated output, a Node job can open the rendered route with Playwright or Puppeteer, inject SnapDOM and capture inside that browser page.
Yes. Replace download format png with svg, or call snapdom.toSvg(ref.current).
Install SnapDOM and turn the DOM your users already see into a portable image.
Open the demoInstall from npm