Fixed bug of stale closure in markdown editor (#2058)

This commit is contained in:
Harshil Sharma 2022-01-08 00:59:57 +05:30 committed by GitHub
parent e1d5e77e0e
commit 155bf75718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -46,10 +46,21 @@ const MarkdownEditorInput = (props: Props): ReactElement => {
}))
, [workspaceUsers])
const ref = useRef<Editor>(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)