From 155bf757187463c1b76b59e132ef5db9e8242e97 Mon Sep 17 00:00:00 2001 From: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com> Date: Sat, 8 Jan 2022 00:59:57 +0530 Subject: [PATCH] Fixed bug of stale closure in markdown editor (#2058) --- .../markdownEditorInput/markdownEditorInput.tsx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/webapp/src/components/markdownEditorInput/markdownEditorInput.tsx b/webapp/src/components/markdownEditorInput/markdownEditorInput.tsx index f362a3c82..85a60c573 100644 --- a/webapp/src/components/markdownEditorInput/markdownEditorInput.tsx +++ b/webapp/src/components/markdownEditorInput/markdownEditorInput.tsx @@ -46,10 +46,21 @@ const MarkdownEditorInput = (props: Props): ReactElement => { })) , [workspaceUsers]) const ref = useRef(null) - const [editorState, setEditorState] = useState(() => { - const state = EditorState.createWithContent(ContentState.createFromText(initialText || '')) + + const generateEditorState = (text?: string) => { + const state = EditorState.createWithContent(ContentState.createFromText(text || '')) return EditorState.moveSelectionToEnd(state) + } + + const [editorState, setEditorState] = useState(() => { + return generateEditorState(initialText) }) + + // avoiding stale closure + useEffect(() => { + setEditorState(generateEditorState(initialText)) + }, [initialText]) + const [isMentionPopoverOpen, setIsMentionPopoverOpen] = useState(false) const [isEmojiPopoverOpen, setIsEmojiPopoverOpen] = useState(false) const [suggestions, setSuggestions] = useState(mentions)