Editor.tsx 728 B

12345678910111213141516171819202122232425
  1. import { VFC, useRef, useState, useEffect } from 'react';
  2. import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
  3. import styles from './Editor.module.css';
  4. export const Editor: VFC = () => {
  5. const [editor, setEditor] = useState<monaco.editor.IStandaloneCodeEditor | null>(null);
  6. const monacoEl = useRef(null);
  7. useEffect(() => {
  8. if (monacoEl) {
  9. setEditor((editor) => {
  10. if (editor) return editor;
  11. return monaco.editor.create(monacoEl.current!, {
  12. value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
  13. language: 'typescript'
  14. });
  15. });
  16. }
  17. return () => editor?.dispose();
  18. }, [monacoEl.current]);
  19. return <div className={styles.Editor} ref={monacoEl}></div>;
  20. };