Skip to content

Commit b6cc349

Browse files
committed
fix: fix ref issue.
1 parent e51fa6b commit b6cc349

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

core/src/index.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef, useImperativeHandle } from 'react';
1+
import React, { useRef, forwardRef, useImperativeHandle } from 'react';
22
import { EditorState, EditorStateConfig, Extension } from '@codemirror/state';
33
import { EditorView, ViewUpdate } from '@codemirror/view';
44
import { useCodeMirror } from './useCodeMirror';
@@ -71,7 +71,7 @@ export interface ReactCodeMirrorRef {
7171
view?: EditorView;
7272
}
7373

74-
const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props, ref) => {
74+
const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props, ref) => {
7575
const {
7676
className,
7777
value = '',
@@ -118,11 +118,13 @@ const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProp
118118
onUpdate,
119119
extensions,
120120
});
121-
useImperativeHandle(ref, () => ({ editor: container, state, view }), [container, state, view]);
122-
useEffect(() => {
123-
setContainer(editor.current);
124-
// eslint-disable-next-line react-hooks/exhaustive-deps
125-
}, []);
121+
122+
useImperativeHandle(ref, () => ({ editor: editor.current, state: state, view: view }), [
123+
editor,
124+
container,
125+
state,
126+
view,
127+
]);
126128

127129
// check type of value
128130
if (typeof value !== 'string') {

core/src/useCodeMirror.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function useCodeMirror(props: UseCodeMirror) {
3232
basicSetup: defaultBasicSetup = true,
3333
root,
3434
} = props;
35-
const [container, setContainer] = useState(props.container);
35+
const [container, setContainer] = useState<HTMLDivElement>();
3636
const [view, setView] = useState<EditorView>();
3737
const [state, setState] = useState<EditorState>();
3838
const defaultLightThemeOption = EditorView.theme(
@@ -122,12 +122,14 @@ export function useCodeMirror(props: UseCodeMirror) {
122122
}
123123
return () => {
124124
if (view) {
125+
setState(undefined);
125126
setView(undefined);
126127
}
127128
};
128-
// eslint-disable-next-line react-hooks/exhaustive-deps
129129
}, [container, state]);
130130

131+
useEffect(() => setContainer(props.container!), [props.container]);
132+
131133
useEffect(
132134
() => () => {
133135
if (view) {

0 commit comments

Comments
 (0)