slate: shouldNodeComponentUpdate doesnt catch document.data update
Do you want to request a feature or report a bug?
Potential bug
What’s the current behavior?
I have some global settings in slate object which I keep in document.data. Some nodes depend on those settings and should rerender on change.
I implemented the following function to make it rerender:
shouldNodeComponentUpdate: (previousEditorProps, editorProps) => {
if (
previousEditorProps.editor.value.document.data.get('variables') !==
editorProps.editor.value.document.data.get('variables')
) {
console.log('variable changed, lets rerender');
return true;
}
return undefined;
},
The problem is that this function will never be called with old and new variables, on change previousEditorProps already contains new variables. Is it intended behaviour or a bug? If this is a bug, I will make some interactive example.
What’s the expected behavior?
previousEditorProps should be called once with old and new variables which would allow me to rerender all custom nodes.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 15 (13 by maintainers)
@ianstormtaylor I didn’t hang onto my
shouldNodeComponentUpdateimplementation for anything since it never really solved my use case, but here’s an example (among several) of where I’m now using context to update components when relevant data changes (without something that normally triggers a re-render).Here,
BlockContextpasses its consumers abeginKey, which is set to the key of the beginning delimiter’s text node for the currently-focused “greater block” (a blockquote, code block, etc.)My
BeginKeywordcomponent switches to the non-Emptyfont (text becomes visible) whenever the related greater block is focused, then switches back toEmptyif focus moves to another area outside the block.You can see it in action by focusing the
QUOTEorSRCblock here (hard refresh if you visited before).Before, I would have tried using
editor.propsto passbeginKeyintoshouldNodeComponentUpdate, then compare to the previous value and see if it had changed. I would re-render all relevant nodes when it changed. In the case of the one above, it would determine ifbeginKeymatched during the render. This never really worked as hoped since the previouseditor.propsweren’t stored.Can someone post code of what they were using
shouldNodeComponentUpdatefor before? and the new way they are handling it with context?