Skip to content

Commit fddb47a

Browse files
authored
Merge pull request #839 from zoran995/clean-subscriptions
clean subscriptions in react
2 parents 40035fd + d2ae518 commit fddb47a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

packages/wrapper-react/src/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* ------------------------------------------------------------------------------------------ */
55

66
import * as monaco from '@codingame/monaco-vscode-editor-api';
7-
import React, { CSSProperties, useCallback, useEffect, useRef, useState } from 'react';
7+
import React, { CSSProperties, useCallback, useEffect, useRef } from 'react';
88
import { didModelContentChange, MonacoEditorLanguageClientWrapper, TextChanges, TextModels, WrapperConfig } from 'monaco-editor-wrapper';
99

1010
export type MonacoEditorProps = {
@@ -29,7 +29,7 @@ export const MonacoEditorReactComp: React.FC<MonacoEditorProps> = (props) => {
2929

3030
const wrapperRef = useRef<MonacoEditorLanguageClientWrapper>(new MonacoEditorLanguageClientWrapper());
3131
const containerRef = useRef<HTMLDivElement>(null);
32-
const [onTextChangedSubscriptions, setOnTextChangedSubscriptions] = useState<monaco.IDisposable[]>([]);
32+
const onTextChangedSubscriptions = useRef<monaco.IDisposable[]>([]);
3333

3434
useEffect(() => {
3535
return () => {
@@ -95,7 +95,7 @@ export const MonacoEditorReactComp: React.FC<MonacoEditorProps> = (props) => {
9595
didModelContentChange(textModels, wrapperConfig.editorAppConfig?.codeResources, onTextChanged);
9696
}));
9797
}
98-
setOnTextChangedSubscriptions(newSubscriptions);
98+
onTextChangedSubscriptions.current = newSubscriptions;
9999
// do it initially
100100
didModelContentChange(textModels, wrapperConfig.editorAppConfig?.codeResources, onTextChanged);
101101
}
@@ -137,10 +137,10 @@ export const MonacoEditorReactComp: React.FC<MonacoEditorProps> = (props) => {
137137
}, []);
138138

139139
const disposeOnTextChanged = useCallback(() => {
140-
for (const subscription of onTextChangedSubscriptions) {
140+
for (const subscription of onTextChangedSubscriptions.current) {
141141
subscription.dispose();
142142
}
143-
setOnTextChangedSubscriptions([]);
143+
onTextChangedSubscriptions.current = [];
144144
}, []);
145145

146146
return (

0 commit comments

Comments
 (0)