Skip to content

Commit 387a0c5

Browse files
committed
chore: Optimize onChange props. (#308)
1 parent c26cba5 commit 387a0c5

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

core/src/useCodeMirror.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useCallback, useEffect, useState } from 'react';
22
import { EditorState, StateEffect } from '@codemirror/state';
33
import { indentWithTab } from '@codemirror/commands';
44
import { EditorView, keymap, ViewUpdate, placeholder } from '@codemirror/view';
@@ -58,11 +58,12 @@ export function useCodeMirror(props: UseCodeMirror) {
5858
maxWidth,
5959
},
6060
});
61+
const handleChange = useCallback((value: string, vu: ViewUpdate) => onChange && onChange(value, vu), []);
6162
const updateListener = EditorView.updateListener.of((vu: ViewUpdate) => {
6263
if (vu.docChanged && typeof onChange === 'function') {
6364
const doc = vu.state.doc;
6465
const value = doc.toString();
65-
onChange(value, vu);
66+
handleChange(value, vu);
6667
}
6768
onStatistics && onStatistics(getStatistics(vu));
6869
});

0 commit comments

Comments
 (0)