From 1aa33634de6507c0adc36ef730d22c74d022f049 Mon Sep 17 00:00:00 2001 From: Trey Smith Date: Tue, 1 Nov 2022 10:29:21 -0400 Subject: [PATCH 1/2] 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. --- .../src/components/Editor.tsx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/samples/browser-esm-vite-react/src/components/Editor.tsx b/samples/browser-esm-vite-react/src/components/Editor.tsx index 71aedd45..821831df 100644 --- a/samples/browser-esm-vite-react/src/components/Editor.tsx +++ b/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(); From 8d313a94721d37b3819e31e490680d4f0fbadb47 Mon Sep 17 00:00:00 2001 From: Trey Smith Date: Wed, 18 Jan 2023 11:13:40 -0500 Subject: [PATCH 2/2] run prettier --- .../browser-esm-vite-react/src/components/Editor.tsx | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/samples/browser-esm-vite-react/src/components/Editor.tsx b/samples/browser-esm-vite-react/src/components/Editor.tsx index 821831df..c4010e6c 100644 --- a/samples/browser-esm-vite-react/src/components/Editor.tsx +++ b/samples/browser-esm-vite-react/src/components/Editor.tsx @@ -8,16 +8,13 @@ export const Editor: VFC = () => { useEffect(() => { if (monacoEl) { - setEditor(editor => { - - if(editor) - return; - + setEditor((editor) => { + if (editor) return; + return monaco.editor.create(monacoEl.current!, { value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'), language: 'typescript' - }) - + }); }); }