1

我想将 SlateJS 编辑器的值存储在 redux 中而不是状态中,但是当我更改hasLinks方法时,我立即收到一个崩溃说明:

TypeError:无法读取未定义的属性“内联”

编辑hasLinks方法

hasLinks = () => {
    // const { value } = this.state // Original
    const { value } = this.props // Update for redux
    // Alternative attempts
    // const { value }  = this.props.editorValue
    // const  value  = this.props.editorValue
    // const { value }  = this.props.editorValue
    // const value  = Object.assign({}, this.props.editorValue)
    // const value  = Value.fromJSON(Object.assign({}, this.props.editorValue))
    return value.inlines.some(inline => inline.type == 'link') // Crashes on this line
    // return value && value.inlines && value.inlines.some(inline => inline.type == 'link') // Alternative attempt that avoids initial crash, but creates a memory overload when editor is accessed much
}

Redux 商店

const initialState = {
    editorValue: Value.fromJSON(initialValue),
}

初始状态

{
    "document": {
        "nodes": [{
                "object": "block",
                "type": "paragraph",
                "nodes": [{
                    "object": "text",
                    "leaves": [{
                        "text": "By default, pasting content into a Slate editor will use the content's plain text representation. This is fine for some use cases, but sometimes you want to actually be able to paste in content and have it parsed into blocks and links and things. To do this, you need to add a parser that triggers on paste. This is an example of doing exactly that!"
                    }]
                }]
            },
            {
                "object": "block",
                "type": "paragraph",
                "nodes": [{
                    "object": "text",
                    "leaves": [{
                        "text": "Try it out for yourself! Copy and paste some rendered HTML content (not the source code) from another site into this editor."
                    }]
                }]
            }
        ]
    }
}

谁能帮我理解为什么这不起作用以及我可以做些什么来更新以解决它?

4

1 回答 1

0

这已经有一年多了,但问题是它const { value } = this.props需要props一个名为value. 你的看起来像editorValue。对象解构仅在具有相同属性名称的情况下(以这种最小的形式)有效(即您不能使用不同的名称重新分配某些东西),这是一个常见的问题。

于 2019-10-02T15:33:50.730 回答