How to Convert a React Component to an Image

saransh kataria
JavaScript in Plain English
2 min readSep 28, 2021

--

Sometimes you want to give the users the ability to download a part of the web application as an image. In that case, you want a way to convert a React component to an image. And it can be straightforward by using a third-party NPM package called html2canvas. Let us look at how to do it.

Setup

We want to first mark the div in HTML markup that we want to be downloaded when the user hits the download button. This could be a chart, user data, a table, or anything we want. We will allocate an id to that element.

<div id="print">This will be downloaded as an image</div>

We can wrap this as a React component with an event handler attached to a download button:

const App = () => {
const handleImageDownload = () => {
// TODO: add logic here
};
return (
<>
<button type="button" onClick={handleImageDownload}>Download</button>
<div id="print">This will be downloaded as an image</div>
</>
);
}

Programming logic

As we discussed earlier, we will install thehtml2canvas NPM package.

npm install html2canvas

Then, all we need to do is use the package to fetch the corresponding div that we want to convert to an image. We then create a link in memory to download the image, click it programmatically and then remove the link from the DOM.

const handleDownloadImage = () => {
const element = document.getElementById('print'),
canvas = await html2canvas(element),
data = canvas.toDataURL('image/jpg'),
link = document.createElement('a');

link.href = data;
link.download = 'downloaded-image.jpg';

document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};

And that is all we need to do to convert a React component to an image! Hope you found this post useful.

Originally published at https://www.wisdomgeek.com on September 28, 2021.

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

--

--

For over and over, I have failed & in the process of losing it all, I realized that I might actually win.