question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

🐛 exposed Monaco Editor and `onChange` do not reflect actual state of Monaco contents

See original GitHub issue

Describe the bug

The underlying Monaco state in React is not accurately reflected in scenarios where users make a change, and then quickly un-make that change. This state does not ever resolve itself unless a user intercepts and makes an additional change.

It’s possible this is a bug in Monaco itself, but hopefully upgrading the version of Monaco could help resolve this.

To Reproduce

Steps to reproduce the behavior:

  1. Put a console.log(contents) in the onChange= of Editor
  2. Type a character (like “T”)
  3. Quickly hit backspace
  4. Notice that onChange only fires once.

You could also grab the editor object from onMount and run getValue() at this time – note that the file contents will still have a “T” in them from your edited changes.

(If you keep alternating between “T” and “Backspace” you will notice that it will fire onChange for every other change, the addition of the “T”, or if you type a T … wait a few seconds and then start alternating “Backspace” and then “T” you will only get the backspace onChange events.)

Expected behavior Any change should be captured accurately in onChange. At a minimum, the exposed editor object should be able to get the accurate file contents in these “change/unchange” scenarios

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
heysweetcommented, Oct 20, 2022

@suren-atoyan I’ll outline my understanding and hopefully this can help!

We are avoiding use of the value= param altogether in favor of interacting with monaco itself because of this issue –

defaultValue= works as expected! value= has a lingering state problem that makes things difficult for us. We’re writing a Web IDE that has git interactions, i.e. at a minimum example, the ability to revert. I’ve approached value= with 3 strategies, none of which works:

  1. Only update value= explicitly when we want to override what the user has in there. This works fairly well but has a big problem. Let’s say that the last commit was just the string “A”. (value="A"). The user replaces this with “B”. (value="A"). When the user attempts to hit the revert button, we have no means of interfacing with the react component to tell it “update your value!” since the value we want to update it to is the already the value being passed in.

  2. Attempt to update value= all the time to avoid this earlier problem. Every keystroke, we call a debounce function which writes to our file cache, so ~50ms later we get the state the user had typed in. If we pass in this updated value into value= while the user is typing, we will continually overwrite what the user is typing, or even if we shorten this feedback loop, we had a number of bugs reported around the cursor jumping around while the user is typing because of this configuration

  3. Only set value= to a string for 1 render in order to force an overwrite and then switch value={undefined} for all other times. This was difficult to manage state and ultimately anti-react-pattern-y so ultimately we’ve decided to fully abandon using the value/defaultValue interface in favor of just interfacing with the monaco instance and its models directly.

1reaction
suren-atoyancommented, Sep 29, 2022

I ended up getting into a bad state with value= and infinite re-render but this could be on my side.

@heysweet this is really important, let me know once you have more information about this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · suren-atoyan/monaco-react - GitHub
Monaco Editor for React - use the monaco-editor in any React application ... 1. 🐛 exposed Monaco Editor and onChange do not reflect...
Read more >
Monaco Editor 'onChange' event? - Stack Overflow
And I'm unable to figure out how to create a handler for whenever the text in the editor window is changed, either by...
Read more >
Monaco Editor for React. - Morioh
onChange (newValue, event) an event emitted when the content of the current model has changed. editorWillMount(monaco) an event emitted before the editor ......
Read more >
PhK - River Thames Conditions - Environment Agency - GOV.UK
The brampton guardian real estate, Amberen best price, Word you can make with, ... Alvin house illinois state university, Hm70 sata drivers, No...
Read more >
React Monaco - StackBlitz
console.log('onChange', newValue, e);. } render() {. const code = this.state.code;. const options = {. selectOnLineNumbers: true. }; return (. <MonacoEditor.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found