@@ -65,6 +65,17 @@ function useCodeEditorContext() {
65
65
return context
66
66
}
67
67
68
+ /**
69
+ * NOTE: once monaco editor mounts it doesn't update it's internal onChange state.
70
+ * Since most of the time onChange methods are declared inside the render body of a component
71
+ * we should use the latest references of onChange.
72
+ * Please see: https://github.com/react-monaco-editor/react-monaco-editor/issues/704
73
+ * Thus as a hack we are using this objects reference to call the latest onChange reference
74
+ */
75
+ const _onChange = {
76
+ onChange : null
77
+ }
78
+
68
79
const CodeEditor : React . FC < CodeEditorInterface > & CodeEditorComposition = React . memo (
69
80
( {
70
81
value,
@@ -100,7 +111,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
100
111
const monacoRef = useRef ( null )
101
112
const { width, height : windowHeight } = useWindowSize ( )
102
113
const memoisedReducer = React . useCallback ( CodeEditorReducer , [ ] )
103
- const [ state , dispatch ] = useReducer ( memoisedReducer , initialState ( { mode, theme, value, diffView, noParsing} ) )
114
+ const [ state , dispatch ] = useReducer ( memoisedReducer , initialState ( { mode, theme, value, diffView, noParsing } ) )
104
115
const [ , json , yamlCode , error ] = useJsonYaml ( state . code , tabSize , state . mode , ! state . noParsing )
105
116
const [ , originalJson , originlaYaml ] = useJsonYaml ( defaultValue , tabSize , state . mode , ! state . noParsing )
106
117
monaco . editor . defineTheme ( CodeEditorThemesKeys . vsDarkDT , {
@@ -193,11 +204,14 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
193
204
editorRef . current . layout ( )
194
205
} , [ width , windowHeight ] )
195
206
207
+ /**
208
+ * NOTE: Please see @_onChange variable
209
+ */
210
+ _onChange . onChange = onChange
211
+
196
212
const setCode = ( value : string ) => {
197
213
dispatch ( { type : 'setCode' , value } )
198
- if ( onChange ) {
199
- onChange ( value )
200
- }
214
+ _onChange . onChange ?.( value )
201
215
}
202
216
203
217
useEffect ( ( ) => {
0 commit comments