Skip to content

Instantly share code, notes, and snippets.

@Houserqu
Created March 31, 2022 02:56
Show Gist options
  • Save Houserqu/95d22e1cb69c3e3242077eb9cdf75457 to your computer and use it in GitHub Desktop.
Save Houserqu/95d22e1cb69c3e3242077eb9cdf75457 to your computer and use it in GitHub Desktop.
react-monaco-editor
import { useEffect, useRef } from "react"
import * as monaco from 'monaco-editor'
interface Props {
defaultValue?: string
height?: number
onChange?: any
}
export default function Editor({defaultValue, height = 500, onChange}: Props) {
const editorRef = useRef<any>(null)
useEffect(() => {
const editor = monaco.editor.create(editorRef.current, {
value: defaultValue,
language: 'javascript',
automaticLayout: true,
});
editor.onDidChangeModelContent((event) => {
onChange(editor.getValue(), event);
});
}, [])
return (
<div ref={editorRef} style={{height}}></div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment