suren-atoyan / monaco-react

Monaco Editor for React - use the monaco-editor in any React application without needing to use webpack (or rollup/parcel/etc) configuration files / plugins
https://monaco-react.surenatoyan.com/
MIT License
3.67k stars 262 forks source link

Uncaught promise when using useMonaco() #440

Open willdady opened 1 year ago

willdady commented 1 year ago

When I add these lines:

  const monaco = useMonaco();
  useEffect(() => {
    if (monaco) {
      console.log('here is the monaco instance:', monaco);
    }
  }, [monaco]);

I get the following error in my browser console:

Uncaught (in promise) {type: 'cancelation', msg: 'operation is manually canceled'}
suren-atoyan commented 1 year ago

Do you use React 18 with strict mode?

willdady commented 1 year ago

Yes, 18.2. It's a Next.js (13.0.7) app. Not sure about strict mode.

suren-atoyan commented 1 year ago

could you please check this?

stefanbugge commented 1 year ago

I get the same error in the console. I am using React 18 in strict mode but not Next.js

cbn-falias commented 1 year ago

Same error but probably a different case with React 18 and no Next.js.

If I don't load it lazy it would work but that's not an option in my project.


Works:

Doesn't work:

AtharvaUmbarkar commented 1 year ago

I am also getting the same error. I am using NextJS 13 with the useMonaco() hook, with Strict Mode: On. Btw, I am already using the "use client" directive on top of the code given below to render the component client side.

image Here's how I am using it. This usage also gives the "default props on functional components are depreciated" warning

mathieu-anderson commented 1 year ago

We are encountering this issue as well: https://github.com/aiven/klaw/issues/1464

I have been able to ascertain that this console.error seems to happen unconditionally when calling useMonaco: in this minimal example, it seems to always be there https://codesandbox.io/s/sleepy-moore-3mct7n?file=/src/App.js:123-132

mathieu-anderson commented 1 year ago

I also ascertained that the console.error only happens when using StrictMode.

wb-wenbei commented 7 months ago

The same error with React 18 in strict mode, not Next.js

moiri commented 3 weeks ago

I get the same error when updating the JSON schema with useEffect().

import React, { useEffect } from 'react';
import Editor, { useMonaco } from '@monaco-editor/react';

interface IConfigEditor {
    value: string;
    path?: string;
    schemaUri?: string;
}

export const ConfigEditor: React.FC<IConfigEditor> = props => {
    const monaco = useMonaco();

    useEffect(() => {
        if (monaco && props.path && props.schemaUri) {
            monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
                validate: true,
                enableSchemaRequest: true,
                schemas: [
                    {
                        uri: props.schemaUri,
                        fileMatch: [props.path],
                    },
                ],
            });
        }
    }, [monaco, props.path, props.schemaUri]);
    return (
        <Editor
            path={props.path}
            language="json"
            value={props.value}
        />
    )
};

Edit: I solved the problem in non-strict mode by using the beforeMount handler of the Editor component to initialize the schemas and a useEffect() hook to update the schema list. It still fails in strict mode, though.