How to Receive and Display a Pasted Image in React
An image pasted into an input field can be taken out of e.clipboardData.files, which you get in an onPaste handler. If you call preventDefault() only when an image is present, text is still pasted into the input field as usual. To display the image, use a URL created with URL.createObjectURL(), and release it in the cleanup of useEffect.
I built a component that adds image pasting to a comment field, and checked what gets pasted and how it is displayed, one step at a time.
The test environment was Ubuntu 24.04, Node.js 22.22.0, and Chromium 153. I used react / react-dom 19.3.1, @types/react 19.3.0, typescript 7.0.2, esbuild 0.28.2, and Playwright 1.63.0. I type-checked the .tsx with tsc, transpiled it with esbuild, and drove Chromium with Playwright. The images to paste were PNGs drawn on a canvas in the page and put on the clipboard with navigator.clipboard.write(), and pasting was done with the Ctrl+V key. I did not test screenshots from the OS, images copied from other apps, Firefox, Safari, or pasting on smartphones.

A pasted image is in clipboardData.files
In React, you can read e.clipboardData from the event passed to a handler given to onPaste. Its contents are a DataTransfer, the same type as dataTransfer used for drag and drop. The MDN description of DataTransfer.files also says that files can be read from clipboardData in a paste handler.
When I printed clipboardData while pasting an image and then text into the input field, I got the following.
--- 1. Paste an image ---
paste イベントの中身: {"target":"textarea","types":["Files"],"files":["image.png:image/png:254"],"items":["file:image/png"]}
--- 3. Paste text ---
paste イベントの中身: {"target":"textarea","types":["text/plain"],"files":[],"items":["string:text/plain"]}
For the image, files contains one File. It also shows up in items as an entry whose kind is file. For the text, files is empty.
In the Chromium I tested, the second pasted image was also named image.png. Since the name does not tell images apart, the component in this article assigns a number to each paste and uses it as the key.
A component that adds image pasting to an input field
PasteImageBox is a component with a textarea for writing a comment and a list of pasted images. Images are kept as File objects in state, and display is left to a Preview for each row of the list.
import { useEffect, useState, type ClipboardEvent } from 'react';
type Pasted = { id: number; file: File };
let nextId = 1;
// Create a display URL from the File and release it when no longer used
function useObjectUrl(file: File) {
const [url, setUrl] = useState<string>();
useEffect(() => {
const objectUrl = URL.createObjectURL(file);
setUrl(objectUrl);
return () => URL.revokeObjectURL(objectUrl);
}, [file]);
return url;
}
function Preview({ file, onRemove }: { file: File; onRemove: () => void }) {
const url = useObjectUrl(file);
return (
<li>
{url && <img src={url} alt={`貼り付けた画像(${file.name})`} width={120} />}
<span>
{file.name}({file.type}、{file.size}バイト)
</span>
<button type="button" onClick={onRemove}>
削除
</button>
</li>
);
}
export function PasteImageBox() {
const [text, setText] = useState('');
const [images, setImages] = useState<Pasted[]>([]);
const [message, setMessage] = useState('');
const handlePaste = (e: ClipboardEvent<HTMLTextAreaElement>) => {
const files = [...e.clipboardData.files].filter((file) => file.type.startsWith('image/'));
// If there is no image, do nothing. Text is entered into the input field as usual
if (files.length === 0) return;
// When it's an image, stop anything from being entered into the input field
e.preventDefault();
setImages((prev) => [...prev, ...files.map((file) => ({ id: nextId++, file }))]);
setMessage(`画像を${files.length}枚貼り付けました`);
};
const remove = (id: number) => {
setImages((prev) => prev.filter((image) => image.id !== id));
};
return (
<div className="box">
<label htmlFor="comment">コメント(画像も貼り付けられます)</label>
<textarea
id="comment"
value={text}
onChange={(e) => setText(e.target.value)}
onPaste={handlePaste}
rows={4}
/>
<p role="status">{message}</p>
<ul>
{images.map(({ id, file }) => (
<Preview key={id} file={file} onRemove={() => remove(id)} />
))}
</ul>
</div>
);
}
handlePaste takes only the entries in files whose type starts with image/. When I pasted two images in a row, both were added to the list and loaded into img.
貼り付けたあと: {"list":["image.png(image/png、254バイト)","image.png(image/png、170バイト)"],"imgs":[{"loaded":true,"w":80,"h":60},{"loaded":true,"w":40,"h":40}]}
The delete control is a button, so it can be operated with the keyboard too. After pasting an image, pressing Tab moved focus to the Delete button, and pressing Enter removed the image from the list.
Call preventDefault only when there is an image
According to the MDN description of the paste event, in editable places such as a textarea, the default browser behavior is to insert the clipboard contents at the caret. To stop this, call preventDefault().
When handlePaste finds no image, it returns without doing anything. So when text is pasted, the default behavior runs and the text goes into the input field. In my test, pasting "This is a photo of a cat" put it into the input field, and the image list did not change.
貼り付けたあと: {"textarea":"猫の写真です","list":["image.png(image/png、254バイト)","image.png(image/png、170バイト)"]}
When only an image was pasted, the input field stayed empty even in the version that does not call preventDefault(). The difference shows up when the clipboard holds both text and an image. As the MDN description of ClipboardItem explains, one clipboard item can hold the same content in several formats. In my test, I put text and a PNG into a single ClipboardItem and pasted it.
--- 4. Paste a clipboard containing both text and an image ---
paste イベントの中身: {"target":"textarea","types":["text/plain","Files"],"files":["image.png:image/png:137"],"items":["string:text/plain","file:image/png"]}
貼り付けたあと: {"textarea":"","list":["image.png(image/png、254バイト)","image.png(image/png、170バイト)","image.png(image/png、137バイト)"]}
--- 6. Version that does not call preventDefault when pasting an image ---
画像だけのとき: 入力欄="" 一覧=["image.png"]
文字と画像が両方のとき: 入力欄="表のセルの文字" 一覧=["image.png","image.png"]
With PasteImageBox, only the image was added to the list, and the input field stayed empty. In the version that does not call preventDefault(), I first pasted an image alone and then pasted an item containing both text and an image. In the second case, the image was added to the list and the text also went into the input field. Which behavior you want depends on the screen you build. If you do not want text inserted when you are taking the image as an image, stop the default as this article does. If you want both, delete the preventDefault() line.
Create the display URL in an effect and release it
Since img's src takes a URL, URL.createObjectURL() creates a URL starting with blob:. According to the memory management section of the MDN blob URL description, each call creates a new URL, and while the URL is valid, the underlying data is kept in memory. It is released when the page is closed, but MDN recommends calling URL.revokeObjectURL() once you are done with it.
useObjectUrl creates the URL inside useEffect and releases it in the cleanup function. When an image is removed from the list, Preview unmounts, and the cleanup runs at that point. In my test, after deleting the first image, I fetched each URL: only the deleted image's URL could no longer be read.
削除した画像の URL: 読めない(TypeError)
残っている画像の URL: 読める(170バイト)
Creating the URL during render makes images disappear in development
You could also create the URL during render with useMemo and release it in a useEffect cleanup.
function MemoPreview({ file }: { file: File }) {
const url = useMemo(() => URL.createObjectURL(file), [file]);
useEffect(() => () => URL.revokeObjectURL(url), [url]);
return <img src={url} alt="memo" width={120} />;
}
With this version, images do not appear when the development build runs inside StrictMode. As the StrictMode reference describes, in development every effect's setup and cleanup run one extra time. The first cleanup revokes the URL, and the second setup does not create a new one, so img is left holding a revoked URL. I displayed the same image with both approaches, and the results were as follows.
--- 7. Compare where the URL is created in the development build (StrictMode) ---
画像の読み込み: ["memo: 表示されない(naturalWidth=0)","effect: 表示された(naturalWidth=50)"]
本番用ビルドでは: ["memo: 表示された(naturalWidth=50)","effect: 表示された(naturalWidth=50)"]
The production build displayed both, so the problem looks limited to development. Still, if you put creating and releasing the URL in the same effect, the setup after the cleanup creates the URL again, so a revoked URL never stays in the display.
Receiving pastes anywhere on the page
A textarea with onPaste only receives pastes made while it has focus. In my test, I clicked an empty part of the page and pressed Ctrl+V. A paste event fired, but nothing was added to the PasteImageBox list.
According to the MDN description of the paste event, the event bubbles up to Document. If you want to receive pastes wherever they happen on the page, attach a listener to document in useEffect instead of using onPaste on PasteImageBox. In this version, the received images are added to the files state.
useEffect(() => {
const onPaste = (e: globalThis.ClipboardEvent) => {
if (!e.clipboardData) return;
const images = imagesOf(e.clipboardData);
if (images.length === 0) return;
e.preventDefault();
setFiles((prev) => [...prev, ...images]);
};
document.addEventListener('paste', onPaste);
return () => document.removeEventListener('paste', onPaste);
}, []);
imagesOf is a function that takes only the images from clipboardData.files, the same as in handlePaste. In this version, an image pasted where nothing was focused was also added to the list. If there is no image, nothing happens, so when I pasted text into the subject field placed on this version's component, it went in as usual.
document で受ける版の一覧: [ 'image.png' ]
件名の入力欄に文字を貼り付けたあと: "お知らせ"
An image received through a paste is an ordinary File. PasteImageBox keeps the File as is in the list state, so, as in the earlier article "How to Upload a File in React and Show Its Progress", you can add the File to FormData and send it to the server.





