JedWatson/react-codemirror

Question: how to get the CodeMirror instance

YilinGao opened this issue · 1 comments

Hello I'm new to react and codemirror, and I am having a problem with your package: how to get the underlying CodeMirror instance when I need to use some of CodeMirror's methods?

import CodeMirror from 'react-codemirror';
// I tried the following line but I got an error
// TypeError: _reactCodemirror2.default.getCodeMirror is not a function
console.log(CodeMirror.getCodeMirror()); 

Thanks for any help!

I'm using TypeScript, and importing it with the current type defintions didn't work so I used require instead: const CodeMirror = require('react-codemirror')

Then in my JSX I use: <CodeMirror ref={(c: any) => this.cm = c} value={this.props.code} />, which gives me the React CodeMirror component on this.cm.

Then in callback handlers, I can reference this.cm.getCodeMirrorInstance() to call the methods like CodeMirror.Doc() to create a new doc, and this.cm.getCodeMirror() to make calls documented in the CodeMirror help like cm.swapDoc(), which I call with essentially this.cm.getCodeMirror().swapDoc().

I find the names getCodeMirror() and getCodeMirrorInstance() confusing though, since the former returns the instance of CodeMirror that is created from my JSX and the latter seems to be similar to static class methods.