Skip to content

Error because this.cursorPos is undefined #3

@stiang

Description

@stiang

First of all - thanks for stepping up now that react-codemirror appears to be unmaintained!

I get an error when I include a <CodeMirror> element. The reason appears to be that this.cursorPos is undefined, but is still being used as an argument for this.editor.setCursor().

Looking at the source, it will indeed be undefined if the first outer if statement doesn’t trigger, but the second outer does:

componentWillReceiveProps(nextProps) {

    if (this.props.value !== nextProps.value) {
      this.hydrated = false;

      if (!this.props.resetCursorOnSet) {
        this.cursorPos = this.editor.getCursor();
      }
    }

    this.hydrate(nextProps);

    if (!this.props.resetCursorOnSet) {
      this.editor.setCursor(this.cursorPos);
    }
  }

I was able to solve this by initializing this.cursorPos in componentDidMount:

  this.cursorPos = {line: 0, ch: 0};

At this point the library appears to work, but I encountered another problem (the page scrolls down whenever the prop that controls the editor value is changed, on a page with multiple editors), but I’ll file that as a separate issue if it turns out to be related to the library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions