monaco-react
monaco-react copied to clipboard
`useEditor` hook?
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.
This issue has been marked as stale due to inactivity. It will be closed in 7 days unless further activity occurs.
I assume this is still legit
This issue has been marked as stale due to inactivity. It will be closed in 7 days unless further activity occurs.
Closing due to inactivity. Feel free to reopen if needed.