瀏覽代碼

Fix possible duplicate of editors in vite sample

In testing the vite sample, I could see duplicate editors created on the screen. From what I can tell, this was due to the if statement checking if editor was null in the `useEffect`. However, the editor variable would not be updated on the first re-render causing a double instance. I adjusted the editor check to be inside the `state dispatch` since it would have the most current information. This seems to have fixed the issue on my end.
Trey Smith 2 年之前
父節點
當前提交
1aa33634de
共有 1 個文件被更改,包括 9 次插入4 次删除
  1. 9 4
      samples/browser-esm-vite-react/src/components/Editor.tsx

+ 9 - 4
samples/browser-esm-vite-react/src/components/Editor.tsx

@@ -7,13 +7,18 @@ export const Editor: VFC = () => {
 	const monacoEl = useRef(null);
 
 	useEffect(() => {
-		if (monacoEl && !editor) {
-			setEditor(
-				monaco.editor.create(monacoEl.current!, {
+		if (monacoEl) {
+			setEditor(editor => {
+				
+				if(editor)
+					return;
+				
+				return monaco.editor.create(monacoEl.current!, {
 					value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
 					language: 'typescript'
 				})
-			);
+			
+			});
 		}
 
 		return () => editor?.dispose();