From d9a4abcbf133dfc90501040082c2867e582921d2 Mon Sep 17 00:00:00 2001 From: clean99 Date: Tue, 21 Nov 2023 11:40:20 +0800 Subject: [PATCH] feat: use memo to reduce editor state update --- frontend/src/components/CodeMirror.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend/src/components/CodeMirror.tsx b/frontend/src/components/CodeMirror.tsx index 36093ee..1459dac 100644 --- a/frontend/src/components/CodeMirror.tsx +++ b/frontend/src/components/CodeMirror.tsx @@ -1,4 +1,4 @@ -import { useRef, useEffect } from "react"; +import { useRef, useEffect, useMemo } from "react"; import { EditorState } from "@codemirror/state"; import { EditorView, keymap, lineNumbers, ViewUpdate } from "@codemirror/view"; import { espresso, cobalt } from "thememirror"; @@ -22,7 +22,7 @@ interface Props { function CodeMirror({ code, editorTheme, onCodeChange }: Props) { const ref = useRef(null); const view = useRef(null); - const editorState = EditorState.create({ + const editorState = useMemo(() => EditorState.create({ extensions: [ history(), keymap.of([ @@ -43,7 +43,7 @@ function CodeMirror({ code, editorTheme, onCodeChange }: Props) { } }), ], - }); + }), [editorTheme]); useEffect(() => { view.current = new EditorView({ state: editorState,