question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getEditorState is not a function - when editors are unmounted/remounted

See original GitHub issue

Use-case/Problem

I’m navigating in my pages that contain the editor as readonly and then I’ll update the content again and I receive this bug in console.

image

And this is when I try to apply more styles using inline toolbar.

image

Environments

react 16.2.0 draft-js-plugins-editor2.0.4 draft-js-static-toolbar-plugin 2.0.1

I’m using redux, redux-saga redesigned.

My editor component receive props from another components. See as below:

import React, { Component } from 'react'
import { actions } from 'mirrorx'

import Editor from 'draft-js-plugins-editor'
import createToolbarPlugin from 'draft-js-static-toolbar-plugin'
import 'draft-js-static-toolbar-plugin/lib/plugin.css'

import {
    EditorState,
    convertFromRaw,
    convertToRaw,
} from 'draft-js'

import styles from '../config/editor-styles'


const staticToolbarPlugin = createToolbarPlugin()
const { Toolbar } = staticToolbarPlugin
let plugins = [staticToolbarPlugin]


const handleChangeContent = (content) => {
    const contentState = content.getCurrentContent()
    const rawContent = convertToRaw(contentState)
    const currentContent = JSON.stringify(rawContent)

    actions.editor.setState(currentContent)
}


class MyEditor extends Component {
    constructor(props) {
        super(props)

        const { content, isReadOnly } = props
        
        if (isReadOnly) {
            plugins = []
        }
        
        const editorState = this._mountEditorState(content)

        this.state = {
            editorState,
        }

        this.onFocus = () => this.refs.editor.focus()

        this.onChange = (editorState) => {
            this.setState({ editorState })
            
            handleChangeContent(editorState)
        }
    }
    componentWillReceiveProps(nextProps) {
        const { content, isReadOnly } = nextProps
        if (content && isReadOnly) {
            const editorState = this._mountEditorState(content)

            this.setState({ editorState })
        }
    }
    _mountEditorState(content) {
        let editorState

        try {
            const rawContent = convertFromRaw(JSON.parse(content))
            editorState = EditorState.createWithContent(rawContent)
        } catch (err) {
            editorState = EditorState.createEmpty()
        }

        return editorState
    }
    render() {
        const { isReadOnly } = this.props
        const { editorState } = this.state

        return (
            <div style={styles.root}>

                { !editorState && <div>Loading content...</div> }

                <div style={styles.editor} onClick={this.onFocus}>
                    <Editor
                        readOnly={isReadOnly}
                        editorState={editorState}
                        onChange={this.onChange}
                        ref='editor'
                        spellCheck={true}
                        plugins={plugins}
                    />
                    { !isReadOnly && <Toolbar /> }
                </div>
            </div>
        )
    }
}


export default MyEditor

References:

https://github.com/facebook/draft-js/issues/473#issuecomment-373791338

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
juliankrispelcommented, Apr 5, 2018

yea we need to fix this - thanks for reporting it. If someone could make a story for this case that’d be sweet

2reactions
sahilpaudelcommented, Jun 6, 2020

@francisrod01 how did it go, I am facing the same issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-rte giving TypeError: r.getEditorState is not a function in ...
getEditorState is not a function. When I comment out the react-rte componnet the error no longer occurs. react-rte component
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found