require.config({ paths: { 'vs': 'monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function() {
    window.editor = monaco.editor.create(document.getElementById('container'),                 {
        value: [
            'function x() {',
            '\tconsole.log("Hello world!");',
            '}'
        ].join('\n'),
        language: 'javascript'
    });
});

function save() {
   // get the value of the data
   var value = window.editor.getValue()
   saveValueSomewhere(value);     
}
Answer from Jonathan on Stack Overflow
🌐
npm
npmjs.com › package › @monaco-editor › react
monaco-editor/react
Just check the returned value of useMonaco · import React, { useEffect } from 'react'; import ReactDOM from 'react-dom'; import Editor, { useMonaco } from '@monaco-editor/react'; function App() { const monaco = useMonaco(); useEffect(() => { // do conditional chaining monaco?.languages.typescript.javascriptDefaults.setEagerModelSync(true); // or make sure that it exists by other ways if (monaco) { console.log('here is the monaco instance:', monaco); } }, [monaco]); return <Editor height="90vh" defaultValue="// some comment" defaultLanguage="javascript" />; } const rootElement = document.getElementById('root'); ReactDOM.render(<App />, rootElement);
      » npm install @monaco-editor/react
    
Published   Feb 13, 2025
Version   4.7.0
Author   Suren Atoyan
🌐
GitHub
github.com › suren-atoyan › monaco-react
GitHub - 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 · GitHub
Just check the returned value of useMonaco · import React, { useEffect } from 'react'; import ReactDOM from 'react-dom'; import Editor, { useMonaco } from '@monaco-editor/react'; function App() { const monaco = useMonaco(); useEffect(() => { // do conditional chaining monaco?.languages.typescript.javascriptDefaults.setEagerModelSync(true); // or make sure that it exists by other ways if (monaco) { console.log('here is the monaco instance:', monaco); } }, [monaco]); return <Editor height="90vh" defaultValue="// some comment" defaultLanguage="javascript" />; } const rootElement = document.getElementById('root'); ReactDOM.render(<App />, rootElement);
Starred by 4.7K users
Forked by 318 users
Languages   TypeScript 93.2% | JavaScript 4.6% | CSS 1.2% | HTML 1.0%
🌐
GitHub
github.com › react-monaco-editor › react-monaco-editor
GitHub - react-monaco-editor/react-monaco-editor: Monaco Editor for React. · GitHub
editorDidMount(editor, monaco) an event emitted when the editor has been mounted (similar to componentDidMount of React).
Starred by 4.2K users
Forked by 389 users
Languages   TypeScript 67.0% | JavaScript 31.7% | HTML 1.3%
🌐
UNPKG
unpkg.com › browse › @monaco-editor › react@1.2.1 › README.md
monaco-editor/react/README.md
There is a prop called `editorDidMount`. It gets two arguments: the first one is a function to get editor value, the second one is the editor instance. Here is an example of how you can implement it. You can play with it [here](https://codesandbox.io/s/example-for-issue-2-1hzz8?fontsize=14) ...
🌐
CodeSandbox
codesandbox.io › s › monaco-editorreact---get-value-1hzz8
monaco-editor/react---get-value
October 4, 2021 - Monaco editor wrapper for easy/one-line integration with React applications without need of webpack (or other module bundler) configuration files
Published   Jul 07, 2019
Author   suren-atoyan
Top answer
1 of 3
44
require.config({ paths: { 'vs': 'monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function() {
    window.editor = monaco.editor.create(document.getElementById('container'),                 {
        value: [
            'function x() {',
            '\tconsole.log("Hello world!");',
            '}'
        ].join('\n'),
        language: 'javascript'
    });
});

function save() {
   // get the value of the data
   var value = window.editor.getValue()
   saveValueSomewhere(value);     
}
2 of 3
25

Both the editor and the model support getting the contents:

  • monaco.editor.IModel.getValue()
  • monaco.editor.IStandaloneCodeEditor.getValue()

So as long as you keep a reference to the editor or model you can query the contents:

var editor = monaco.editor.create(...);
var text = editor.getValue();

Or in case of the model:

var model = monaco.editor.createModel(...);
var text = model.getValue();

If you have a diff-editor you cannot access the text directly on the editor but you can access them on the individual models (i.e. through IStandaloneDiffEditor.getModel()):

var diffEditor = monaco.editor.createDiffEditor(...);
var originalText = diffEditor.getModel().original.getValue();
var modifiedText = diffEditor.getModel().modified.getValue();

Or through the different editors (getModifiedEditor() and getOriginalEditor()):

var originalText = diffEditor.getModifiedEditor().getValue();
var modifiedText = diffEditor.getOriginalEditor().getValue();

Just in case you're interested in a part of the text, the model also supports getValueInRange() which gives you the text in a specified range, delimited by a starting and ending column and linenumber, for example:

var editor = monaco.editor.create(...);
var model = editor.getModel();
var partOfTheText = model.getValueInRange({
  startLineNumber: 1,
  startColumn: 2,

  endLineNumber: 3,
  endColumn: 10,
})
🌐
GitHub
github.com › react-monaco-editor › react-monaco-editor › issues › 243
How to set value of the editor programmatically? · Issue #243 · react-monaco-editor/react-monaco-editor
August 27, 2019 - Hi, thanks for the great library. I'm working on End-to-End testing using Cypress and I need to programmatically update the value within the Monaco Editor from my tests. How do I do it? I tried to find the instance at window object but I...
Author   scaryguy
🌐
CodeSandbox
codesandbox.io › s › monaco-editorreact---get-value-forked-1k7g0
monaco-editor/react---get-value (forked)
November 18, 2020 - Monaco editor wrapper for easy/one-line integration with React applications without need of webpack (or other module bundler) configuration files
Published   Nov 18, 2020
Find elsewhere
🌐
Tabnine
tabnine.com › home page › code › javascript › react-monaco-editor
react-monaco-editor JavaScript and Node.js code examples | Tabnine
render() { return ( <MonacoEditor width="600" height="800" language="typescript" theme="vs-dark" defaultValue='' value={this.state.code} onChange={this.onChange} /> ) }
🌐
npm
npmjs.com › package › @monaco-editor › react
@monaco-editor/react - npm
November 6, 2023 - Just check the returned value of useMonaco · import React, { useEffect } from 'react'; import ReactDOM from 'react-dom'; import Editor, { useMonaco } from '@monaco-editor/react'; function App() { const monaco = useMonaco(); useEffect(() => { // do conditional chaining monaco?.languages.typescript.javascriptDefaults.setEagerModelSync(true); // or make sure that it exists by other ways if (monaco) { console.log('here is the monaco instance:', monaco); } }, [monaco]); return <Editor height="90vh" defaultValue="// some comment" defaultLanguage="javascript" />; } const rootElement = document.getElementById('root'); ReactDOM.render(<App />, rootElement);
      » npm install @monaco-editor/react
    
Published   Feb 13, 2025
Version   4.7.0
Author   Suren Atoyan
🌐
Medium
medium.com › @fraiha26 › adding-read-only-sections-in-the-monaco-editor-in-react-part-2-b693cf5a7c92
Adding read-only sections in the Monaco Editor in React Part 2 | by AlexF | Medium
April 20, 2023 - Then, we get the editor's model using the getModel() method of the editor object, and set its value to defaultCode using the setValue() method. Finally, we update the state of the code variable to match the default code.
🌐
Surenatoyan
monaco-react.surenatoyan.com
Monaco Editor React
Monaco editor wrapper for easy integration with React applications
🌐
Stack Overflow
stackoverflow.com › questions › tagged › react-monaco-editor
Newest 'react-monaco-editor' Questions - Stack Overflow
I’m trying to add a custom React component as an overlay widget to the Monaco Editor. I want the widget to appear as a floating overlay on bottom of the editor content, similar to how the built-in ... ... I read the API document I want to get the cursor position (row, column), such as : But seems there is no API for this function, is there any way to implement it?
🌐
Stack Overflow
stackoverflow.com › questions › 69759737 › how-to-use-monaco-editor-in-a-react-functional-component
How to use Monaco Editor in a React functional component?
monaco.editor.create(document.getElementById('container'), { value: [ 'function x() {', '\tconsole.log("Hello world!");', '}' ].join('\n'), language: 'javascript' }); javascript · reactjs · monaco-editor · Share · Improve this question · Follow · asked Oct 28, 2021 at 19:19 ·
🌐
React.js Examples
reactjsexample.com › monaco-editor-for-react
Monaco Editor for React
October 25, 2018 - import React from 'react'; import { render } from 'react-dom'; import MonacoEditor from 'react-monaco-editor'; class App extends React.Component { constructor(props) { super(props); this.state = { code: '// type your code...', } } editorDidMount(editor, monaco) { console.log('editorDidMount', editor); editor.focus(); } onChange(newValue, e) { console.log('onChange', newValue, e); } render() { const code = this.state.code; const options = { selectOnLineNumbers: true }; return ( <MonacoEditor width="800" height="600" language="javascript" theme="vs-dark" value={code} options={options} onChange={::this.onChange} editorDidMount={::this.editorDidMount} /> ); } } render( <App />, document.getElementById('root') );
🌐
GitHub
microsoft.github.io › monaco-editor
Monaco Editor
The index of Microsoft GitHub repositories that are open source has been relocated to https://opensource.microsoft.com
🌐
LogRocket
blog.logrocket.com › home › build a web editor with react-monaco-editor
Build a web editor with react-monaco-editor - LogRocket Blog
June 4, 2024 - In the code block above, we imported the MonacoDiffEditor from the react-monaco-editor package. We then created two lines of code to compare and passed them to the MonacoDiffEditor as props for the value property and the original property. The original property holds the original value and the value property holds the compared code.
🌐
npm
npmjs.com › package › react-monaco-editor
react-monaco-editor - npm
editorDidMount(editor, monaco) an event emitted when the editor has been mounted (similar to componentDidMount of React).
      » npm install react-monaco-editor