mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 15:05:39 +01:00
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:
parent
bc9e8523b5
commit
1aa33634de
1 changed files with 9 additions and 4 deletions
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue