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
UnControlledandControlledvariant based on different use cases. I see you are callingsetStatewithin theonBeforeChangecallback. In your case, I'd suggest leveraging the controlled component as such...With the controlled variant, managing state is required on the
valueprop to see any changes.Additionally, the
UnControlledcomponent also has anonBeforeChangecallback as well, yet with different behavior as such...Here however,
onChangewill be deferred untilnextis invoked ifonBeforeChangeis specified. If not,onChangewill fire regardless. Important to note, though, with theUnControlledvariant, the editor will always react to input changes - the difference will simply be ifonChangeis 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.