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.
This commit is contained in:
Trey Smith 2022-11-01 10:29:21 -04:00 committed by GitHub
parent bc9e8523b5
commit 1aa33634de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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();