I am using react-codemirror node module as follows:
<CodeMirror
className={className}
value={this.state.code}
onBeforeChange={this.onBeforeChange}
onChange={this.onChange}
options={options}
/>
The change
event works fine, but I can't seem to hook up with the beforeChange
event. Anyone know what I am doing wrong?
I have declared handlers in my class as follows:
onBeforeChange(change) {
console.log('calling beforeChange');
}
onChange(newCode) {
this.setState({
code: newCode
});
}
Author of react-codemirror2 here. I stumbled upon your question and wanted to follow up with a detailed answer as there are some breaking changes in 3.x. The component now ships with an
UnControlled
andControlled
variant based on different use cases. I see you are callingsetState
within theonBeforeChange
callback. In your case, I'd suggest leveraging the controlled component as such...With the controlled variant, managing state is required on the
value
prop to see any changes.Additionally, the
UnControlled
component also has anonBeforeChange
callback as well, yet with different behavior as such...Here however,
onChange
will be deferred untilnext
is invoked ifonBeforeChange
is specified. If not,onChange
will fire regardless. Important to note, though, with theUnControlled
variant, the editor will always react to input changes - the difference will simply be ifonChange
is called or not.These changes were inspired due to the needs of the community and I encourage you to open an issue should anything not be working as you expect.