From 29a31450eedaa86e11463d86e4fc70432ca00190 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Thu, 14 Oct 2021 10:01:48 +0800 Subject: [PATCH] fix: Fix value props controlled issue. #199 --- src/useCodeMirror.ts | 8 +++++--- website/App.tsx | 4 +--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/useCodeMirror.ts b/src/useCodeMirror.ts index 906211716..c3e3b19bf 100644 --- a/src/useCodeMirror.ts +++ b/src/useCodeMirror.ts @@ -104,9 +104,11 @@ export function useCodeMirror(props: UseCodeMirror) { useEffect(() => { if (view) { const currentValue = view.state.doc.toString(); - view.dispatch({ - changes: { from: 0, to: currentValue.length, insert: value || '' }, - }); + if (value !== currentValue) { + view.dispatch({ + changes: { from: 0, to: currentValue.length, insert: value || '' }, + }); + } } }, [value, view]); diff --git a/website/App.tsx b/website/App.tsx index 67c51511b..5e48b73e5 100644 --- a/website/App.tsx +++ b/website/App.tsx @@ -193,9 +193,7 @@ export default function App() { autoFocus={autofocus} className={styles.codemirror} placeholder={placeholder} - onChange={(value) => { - // console.log('value:', value); - }} + onChange={(value) => setCode(value)} style={{ maxWidth: '995px', margin: '-18px auto 0 auto',