SlateJS + Redux
Я хочу сохранить значение редактора SlateJS, хранящееся в Redx, а не в состоянии, но я при изменении hasLinks
метод, я сразу получаю аварийное заверение, заявив:
TypeError: Невозможно прочитать свойство inlines неопределенного
редактора 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 Store
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."
}]
}]
}
]
}
}
Может кто-нибудь помочь мне понять, почему это не работает и что я могу сделать, чтобы обновить, чтобы решить эту проблему?
0 ответов
Этому уже больше года, но проблема в том, что const { value } = this.props
требует, чтобы props
имеет свойство под названием value
. Твой выглядит какeditorValue
. Распространенная ошибка, что деструктуризация объекта работает (в этой самой минимальной форме) только с тем же именем свойства (т.е. вы не можете переназначить что-то с другим именем).