@@ -17,7 +17,11 @@ function ReactCodeMirror(props = {}, ref) {
17
17
const { options = { } , value = '' , width = '100%' , height = '100%' } = props ;
18
18
const [ editor , setEditor ] = useState ( ) ;
19
19
const textareaRef = useRef ( ) ;
20
+ const lastestProps = useRef ( props ) ;
21
+
20
22
useImperativeHandle ( ref , ( ) => ( { editor } ) , [ editor ] ) ;
23
+ lastestProps . current = props ;
24
+
21
25
// 将props中所有的事件处理函数映射并保存
22
26
function getEventHandleFromProps ( ) {
23
27
const propNames = Object . keys ( props ) ;
@@ -60,7 +64,7 @@ function ReactCodeMirror(props = {}, ref) {
60
64
const instance = CodeMirror . fromTextArea ( textareaRef . current , { ...defaultOptions , ...options } ) ;
61
65
const eventDict = getEventHandleFromProps ( ) ;
62
66
Object . keys ( eventDict ) . forEach ( ( event ) => {
63
- instance . on ( eventDict [ event ] , props [ event ] ) ;
67
+ instance . on ( eventDict [ event ] , ( ... params ) => lastestProps . current [ event ] ( ... params ) ) ;
64
68
} ) ;
65
69
instance . setValue ( value || '' ) ;
66
70
@@ -103,4 +107,4 @@ function ReactCodeMirror(props = {}, ref) {
103
107
) ;
104
108
}
105
109
106
- export default React . forwardRef ( ReactCodeMirror ) ;
110
+ export default React . forwardRef ( ReactCodeMirror ) ;
0 commit comments