Skip to content

Instantly share code, notes, and snippets.

@panphora
Created December 13, 2024 22:11
Show Gist options
  • Save panphora/16b1e948e0f9313b9927dea7814b85ac to your computer and use it in GitHub Desktop.
Save panphora/16b1e948e0f9313b9927dea7814b85ac to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
const useEditable = (initialText) => {
const [text, setText] = useState(initialText);
const [isEditingDraft, setIsEditingDraft] = useState(false);
const [draftText, setDraftText] = useState(text);
const enableEditingDraft = () => {
setDraftText(text);
setIsEditingDraft(true);
};
const saveDraftText = () => {
setText(draftText);
setIsEditingDraft(false);
};
const discardDraftText = () => {
setIsEditingDraft(false);
};
return {
text,
isEditingDraft,
draftText,
enableEditingDraft,
saveDraftText,
discardDraftText,
setDraftText
};
};
export default useEditable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment