Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix: Solve the problem of abnormal hooks values caused by closures

robin7f3d0a5abb31
diff --git a/ui/src/components/Editor/index.tsx b/ui/src/components/Editor/index.tsx
index 8fe0616e..fcc2d6ec 100644
--- a/ui/src/components/Editor/index.tsx
+++ b/ui/src/components/Editor/index.tsx
@@ -1,7 +1,6 @@
import {
useEffect,
useRef,
- useState,
ForwardRefRenderFunction,
forwardRef,
useImperativeHandle,
@@ -74,14 +73,6 @@ const MDEditor: ForwardRefRenderFunction<EditorRef, Props> = (
autoFocus,
});
- const [markdown, setMarkdown] = useState<string>(value || '');
-
- useEffect(() => {
- if (value !== markdown) {
- setMarkdown(value);
- }
- }, [value]);
-
const getHtml = () => {
return previewRef.current?.getHtml();
};
@@ -103,29 +94,31 @@ const MDEditor: ForwardRefRenderFunction<EditorRef, Props> = (
<>
<div className={classNames('md-editor-wrap rounded', className)}>
<EditorContext.Provider value={editor}>
- <PluginRender
- type="editor"
- className="toolbar-wrap px-3 d-flex align-items-center flex-wrap"
- editor={editor}
- previewElement={previewRef.current?.element}>
- <Heading />
- <Bold />
- <Italice />
- <div className="toolbar-divider" />
- <Code />
- <LinkItem />
- <BlockQuote />
- <Image />
- <Table />
- <div className="toolbar-divider" />
- <OL />
- <UL />
- <Indent />
- <Outdent />
- <Hr />
- <div className="toolbar-divider" />
- <Help />
- </PluginRender>
+ {editor && (
+ <PluginRender
+ type="editor"
+ className="toolbar-wrap px-3 d-flex align-items-center flex-wrap"
+ editor={editor}
+ previewElement={previewRef.current?.element}>
+ <Heading />
+ <Bold />
+ <Italice />
+ <div className="toolbar-divider" />
+ <Code />
+ <LinkItem />
+ <BlockQuote />
+ <Image />
+ <Table />
+ <div className="toolbar-divider" />
+ <OL />
+ <UL />
+ <Indent />
+ <Outdent />
+ <Hr />
+ <div className="toolbar-divider" />
+ <Help />
+ </PluginRender>
+ )}
</EditorContext.Provider>
<div className="content-wrap">
@@ -135,7 +128,7 @@ const MDEditor: ForwardRefRenderFunction<EditorRef, Props> = (
/>
</div>
</div>
- <Viewer ref={previewRef} value={markdown} />
+ <Viewer ref={previewRef} value={value} />
</>
);
};
diff --git a/ui/src/components/Editor/utils/index.ts b/ui/src/components/Editor/utils/index.ts
index 480ed0eb..e0630566 100644
--- a/ui/src/components/Editor/utils/index.ts
+++ b/ui/src/components/Editor/utils/index.ts
@@ -127,7 +127,8 @@ export const useEditor = ({
onFocus,
onBlur,
}) => {
- const [editor, setEditor] = useState<Editor>();
+ const [editor, setEditor] = useState<CodeMirror.Editor | null>(null);
+ const [value, setValue] = useState<string>('');
const onEnter = (cm) => {
const cursor = cm.getCursor();
@@ -168,13 +169,13 @@ export const useEditor = ({
placeholder,
focus: autoFocus,
});
- setEditor(cm);
+ setEditor(cm);
createEditorUtils(codeMirror, cm);
cm.on('change', (e) => {
const newValue = e.getValue();
- onChange?.(newValue);
+ setValue(newValue);
});
cm.on('focus', () => {
@@ -187,8 +188,13 @@ export const useEditor = ({
cm.addKeyMap({
Enter: onEnter,
});
+ return cm;
};
+ useEffect(() => {
+ onChange?.(value);
+ }, [value]);
+
useEffect(() => {
if (!(editorRef.current instanceof HTMLElement)) {
return;