Created
December 13, 2024 22:11
-
-
Save panphora/16b1e948e0f9313b9927dea7814b85ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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