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.82k stars 269 forks source link

Editor cannot be used with Next.js #609

Closed LBBO closed 7 months ago

LBBO commented 7 months ago

Describe the bug In a clean Next.js project, when I npm install @monaco-editor/react and simply try to display an editor somewhere (which should be possible, as per the docs), I get an error

Error: useState only works in Client Components. Add the "use client" directive at the top of the file to use it.

To Reproduce See this CodeSandbox

Steps to reproduce the behavior:

  1. Create fresh Next.js app (I used the CodeSandbo "Next.js (App Router)" template)
  2. Install @monaco-editor/react
  3. Display an <Editor /> somewhere
  4. See above error
  5. Optionally try to wrap the import in a dynamic(..., { ssr: false }), same result

Expected behavior There should be no error, as per the docs linked above.

Desktop (please complete the following information):

Additional context I briefly checked the source code and found a 'use client' at the top of the corresponding TSX files. However, that 'use client' seems to get lost in the build process because it isn't present in the final index.mjs.

suren-atoyan commented 7 months ago

you need to use "use client" at the top of the file where you use the editor

Screenshot 2024-04-23 at 16 08 58