Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

feat: use memo to reduce editor state update

clean99d9a4abcbf133
diff --git a/frontend/src/components/CodeMirror.tsx b/frontend/src/components/CodeMirror.tsx
index 36093eeb..1459dacc 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<HTMLDivElement>(null);
const view = useRef<EditorView | null>(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,