monaco-react icon indicating copy to clipboard operation
monaco-react copied to clipboard

`useEditor` hook?

Open mmkal opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe.

It'd be nice to abstract away the need for useRef/onMount and provide a hook for the editor instance, similar to useMonaco.

Describe the solution you'd like

import React from "react";
import ReactDOM from "react-dom";

import Editor, { useEditor } from "@monaco-editor/react";

function App() {
  const editor = useEditor();

  return (
    <Editor
      height="90vh"
      defaultLanguage="javascript"
      defaultValue="// some comment"
      onChange=(() => {
        editor?.setPosition(...) // do something with editor
      })
    />
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Describe alternatives you've considered

The current docs:

import React, { useRef } from "react";
import ReactDOM from "react-dom";

import Editor from "@monaco-editor/react";

function App() {
  const editorRef = useRef(null);

  function handleEditorDidMount(editor, monaco) {
    // here is the editor instance
    // you can store it in `useRef` for further usage
    editorRef.current = editor; 
  }

  return (
    <Editor
      height="90vh"
      defaultLanguage="javascript"
      defaultValue="// some comment"
      onMount={handleEditorDidMount}
      onChange=(() => {
        editorRef.current?.setPosition(...) // do something with editor
      })
    />
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Additional context

It's more convenient and less error prone to provide a hook like this, I think. And the implementation could probably be doing the same thing as the current recommendation, under the hood. Also, the useEditor return type can be provided easily for typescript and javascript users.

mmkal avatar Feb 03 '22 14:02 mmkal

This issue has been marked as stale due to inactivity. It will be closed in 7 days unless further activity occurs.

github-actions[bot] avatar Apr 26 '25 00:04 github-actions[bot]

I assume this is still legit

mmkal avatar Apr 26 '25 00:04 mmkal

This issue has been marked as stale due to inactivity. It will be closed in 7 days unless further activity occurs.

github-actions[bot] avatar Oct 25 '25 00:10 github-actions[bot]

Closing due to inactivity. Feel free to reopen if needed.

github-actions[bot] avatar Nov 01 '25 00:11 github-actions[bot]