How to Edit Text in Place on Double-Click in React
To let users double-click text to edit it in React, attach onDoubleClick to the element that shows the text. When it fires, switch a piece of state and render an input. This works well when you want to quickly rename an item in a list, such as a ToDo item name, a file name, or a Kanban card title.
Showing the input alone doesn't give it focus, so you can't type right away. If you add code to return focus to the edit button, editing can restart right after Enter closes it. This article builds an EditableText component that fixes these problems.
The test environment was Ubuntu 24.04, Node.js 22.23.4, React 19.3.1, TypeScript 7.0.2, esbuild 0.28.2, Playwright 1.63.0, and Chromium 153. I type-checked the .tsx with tsc, transpiled it with esbuild, and checked the behavior by driving Chromium with Playwright. The results were the same with and without StrictMode, and the console showed no errors or warnings. Japanese text input was reproduced with Chrome DevTools Protocol (CDP) commands, not a real IME. I did not test Firefox, Safari, taps on a phone, or screen reader announcements.

A component that switches to an input on double-click
The component keeps both the display/edit mode and the input's contents in one piece of state, draft. null means display mode, and a string means edit mode. As described in the onDoubleClick entry in React's official reference, onDoubleClick corresponds to the browser's dblclick event.
import { useRef, useState } from 'react';
import { flushSync } from 'react-dom';
type Props = {
value: string;
label: string; // Name for the input and button (for example, the memo title)
onSave: (next: string) => void;
};
export function EditableText({ value, label, onSave }: Props) {
// null means display mode; a string means edit mode (the input's contents)
const [draft, setDraft] = useState<string | null>(null);
const editButtonRef = useRef<HTMLButtonElement>(null);
function finish(save: boolean, returnFocus: boolean) {
if (draft === null) return;
const next = draft.trim();
if (save && next !== '' && next !== value) onSave(next);
if (returnFocus) {
// Remove the input, then move focus back to the edit button
flushSync(() => setDraft(null));
editButtonRef.current?.focus();
} else {
setDraft(null);
}
}
if (draft === null) {
return (
<span className="editable">
<span className="editable-text" onDoubleClick={() => setDraft(value)}>
{value}
</span>
<button
ref={editButtonRef}
type="button"
aria-label={`${label}を編集`}
onClick={() => setDraft(value)}
>
編集
</button>
</span>
);
}
return (
<input
aria-label={label}
autoFocus
value={draft}
onFocus={(e) => e.currentTarget.select()}
onChange={(e) => setDraft(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.nativeEvent.isComposing) {
e.preventDefault(); // Keep Enter from pressing the edit button it returns focus to
finish(true, true);
}
if (e.key === 'Escape') finish(false, true);
}}
onBlur={() => finish(true, false)}
/>
);
}
The component using it only needs to pass the current value and a save handler.
const [title, setTitle] = useState('猫の健康メモ');
// …
<EditableText value={title} label="メモのタイトル" onSave={setTitle} />
Here are the results from operating it in the test environment:
| Action | Result |
|---|---|
| Double-click the text | Switches to an input, which gets focus with all of "Cat health memo" selected |
| Type "Cat weight memo" and press Enter | Saved, and the display returns to "Cat weight memo". Focus is on the edit button |
| Type "unfinished draft" and press Escape | Not saved, and the display stays "Cat health memo". Focus is on the edit button |
| Type "Cat vet visit memo" and click another button | Saved, and focus moves to the clicked button |
| Clear the text and press Enter | Not saved, and the display returns to the original "Cat health memo" |
| Press Enter without typing anything | The save handler is not called, and the display returns |
To prevent empty values from being saved, finish trims surrounding whitespace and does not call onSave if the result is empty or unchanged.
Just showing the input doesn't let you type right away
When I wrote the code to set editing to true on double-click and render the input, the input appeared in the test environment, but focus stayed on body. Typing "X" afterward did not change the input's contents. Only after clicking the input did focus move into it.
So EditableText adds autoFocus to the input. As described in the <input> entry in React's official reference, when autoFocus is true, React focuses the element when it appears on screen. The onFocus handler also calls select() to select all of the text. When you want to replace the whole name, you can just type. To change only part of it, click inside the input again. In the test environment, the selection was cleared and the cursor was placed at the clicked position (after the third character).
Editing restarts after Enter
EditableText calls e.preventDefault() in its Enter handler. I prepared a version without this line. When I typed "Cat weight memo" and pressed Enter, the save handler was called once, but the screen stayed in the input. The saved "Cat weight memo" was fully selected, and edit mode had started again.
I recorded the events that fired after pressing Enter on document. The element names are in lowercase.
["keydown input","focusin button","keypress button","click button","focusin input"]
The keydown handler on the input moved focus to the edit button. As a result, the keypress for the same Enter went to the edit button, which was then pressed (click), so editing restarted. With e.preventDefault() in keydown, the events were only ["keydown input","focusin button"]. There was no keypress or click. The display returned, and focus stayed on the edit button.
Focus is returned to the edit button so that keyboard users don't lose their place after pressing Enter or Escape. When I removed both flushSync and focus(), focus moved to body after both Enter and Escape. While editing, the component renders only the input, so the edit button isn't on screen yet. That's why flushSync first commits the removal of the input and the appearance of the edit button, and only then does focus() run. As described in the flushSync entry in React's official reference, flushSync applies state updates to the DOM immediately. The same page notes that flushSync is often unnecessary and that overusing it can significantly hurt app performance. So I use it only where the DOM must be in place first, as here. In the article "How to make table cells editable in place in React," focus is moved in a useEffect after rendering finishes. This article moves focus inside the Enter and Escape handlers with flushSync.
When you click outside the input, onBlur saves the value, but it does not move focus. The element you clicked already has focus.
The handling of Enter and Escape, clicks outside the input, and not saving on Enter while a Japanese IME is converting text is compared in more detail in the article on editable table cells. The Enter handler in this article only runs when e.nativeEvent.isComposing is false. As described in the MDN entry for isComposing, this property indicates whether a keyboard event happened during IME composition. In the test environment, pressing Enter while the text was still being converted did not save it. After confirming the kanji for "cat," pressing Enter saved "cat."
Adding an edit button for people who can't double-click
Double-clicking is a mouse action and can't be done with the keyboard. So EditableText places an "Edit" button next to the text. The button has an aria-label of "Edit memo title" (メモのタイトルを編集).
In the test environment, pressing Tab once focused the edit button. Pressing Enter there switched to the input with all the text selected, just like a double-click. I typed "Cat vaccination memo" and pressed Enter, and it saved with focus returning to the edit button. Pressing Enter again switched back to the input, and I changed it to "Cat vaccine memo" and saved it. You can keep editing repeatedly without using a mouse.
When you want click and double-click to do different things
You may want a ToDo item to toggle done/not done on click, and enter name editing on double-click. As described in the MDN entry for the dblclick event, dblclick fires after two click events. So if you attach both onClick and onDoubleClick, onClick runs twice for each double-click.
In the test environment, I double-clicked an item whose done state toggled on each click. This is the recorded output.
["click detail=1","done: false -> true","click detail=2","done: true -> false","dblclick -> 編集を始める"]
On the first click, the item became done. On the second click, it returned to not done, and then editing started. Each double-click ran the done toggle twice.
As described in the MDN entry for UIEvent.detail, the detail value of a click event is the number of the current click. When I changed the code to toggle only when detail === 1, the second click no longer toggled. However, the first click still toggled, so an item that was double-clicked remained marked as done.
If you don't want a double-click to toggle at all, delay the click handler briefly and cancel it if a double-click arrives in the meantime.
import { useEffect, useRef, useState } from 'react';
export function TodoTitle() {
const [done, setDone] = useState(false);
const timer = useRef<number | undefined>(undefined);
useEffect(() => () => window.clearTimeout(timer.current), []); // Discard the pending timer when unmounting
return (
<span
style={{ textDecoration: done ? 'line-through' : 'none' }}
onClick={(e) => {
window.clearTimeout(timer.current);
if (e.detail !== 1) return;
timer.current = window.setTimeout(() => setDone((d) => !d), 250);
}}
onDoubleClick={() => {
window.clearTimeout(timer.current);
// Start editing here
}}
>
牛乳を買う
</span>
);
}
In the test environment, after a double-click, the strikethrough never appeared, even after 400 milliseconds, and the done state did not change. With a single click, there was no strikethrough right after the click, and it appeared about 260 milliseconds later. Without the delay, the change was visible in about 20 milliseconds.
This approach has two weaknesses. First, a single click responds later by the length of the delay. Second, if the second click of a double-click arrives later than the delay, the toggle happens first. I used Playwright to send the second click (click count 2) 300 milliseconds after the first one, with the 250-millisecond delay version above. The item toggled to done, and then dblclick arrived and editing started.
If you don't want to delay click responses, you can leave the done toggle to a checkbox and attach only double-click editing to the name text. If you don't put both click and double-click on the same element, you don't need to decide on a delay.





