Re-setting options on an existing instance can have side effects
See original GitHub issueI’ve found that react-codemirror seems incompatible with proper usage of the lint
add-on to CodeMirror.
The way to activate linting is to set it as an option, e.g.
CodeMirror.fromTextArea([…], {
lint: {
predef: ["window"]
}
});
lint’s default behavior is to respond to onChange events (debounced and on a 500ms delay), but when it’s first initialized, it runs immediately on the current editor value. react-codemirror’s approach to setting all of the options whenever the component updates effectively causes lint to fire on every editor change (with no delay) because setting the lint option re-initializes it, even if the option provided is the same.
I have a proof-of-concept that addresses this problem by comparing the editor’s existing option values to the new option values on each update, and only setting each option if it’s different. But, I’m waiting to hear about #49 because the underlying bug behind that pull request prevents options from being applied correctly to multiple instances of CodeMirror.
The approach I had in mind is a bit of a kludge, but I think JSON.stringify would suffice as a way to compare option values quickly:
setCodeMirrorOptionIfChanged: function(optionName, newValue) {
var oldValue = this.codeMirror.getOption(optionName);
if (JSON.stringify(oldValue) !== JSON.stringify(newValue)) {
this.codeMirror.setOption(optionName, newValue);
}
},
Issue Analytics
- State:
- Created 7 years ago
- Comments:8
@justindeguzman I ended up forking this project pretty aggressively since it seemed dormant for awhile. To solve this particular problem I added some functions to test “deep” object/array equality.
@attaboy Got it! Turns out I wasn’t getting the JSHINT function to be globally available, as necessary. Once I figured out how to import it properly, things began working fine. Thanks!