slate: Modification to onChange cannot get latest state

Do you want to request a feature or report a bug?

Bug ๐Ÿ›

Whatโ€™s the current behavior?

If I have a state which should update for each onChange() call on the editor, the onChange() only has access to the initial state. It also somehow reverts to the barebones editor state during this behavior.

Cannot find a descendant at path [0] in node: 
{
  children: [],
  operations: [],
  selection: null,
  marks: null,
  history: {
    undos: [],
    redos: []
  }
}
GIF of issue in action

Apr-17-2020 15-35-34

Sandbox of issue reproduction

https://codesandbox.io/s/slate-repo-irmerk-17-04-2020-mebkd

Information about your OS, browser, Slate version, etc.

Slate: 0.57.1 Browser: Chrome OS: Mac

Whatโ€™s the expected behavior?

Slate should handle updating of the editor without losing its current value, selection, etc when one of the custom high order functions wrapping the creation of the editor changes

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 17 (5 by maintainers)

Most upvoted comments

I am seeing this same behavior whenever my app hot module reloads. In my case the steps to reproduce are:

  1. Begin typing in an editor.
  2. Make a change in code and save to trigger hot module reload.
  3. After a successful bundle rebuild, the page attempts to refresh and the following error is thrown: Cannot find a descendant at path [0] in node: {"children":[],"operations":[],"selection":null,"marks":null}.

This is confusing since I initialize the editor with a default value of [{ type: 'paragraph', children: [{ text: '' }] }], so where is the editor getting the value reported in the error message?

The resolution with issues relating to CRA was to utilize useRef instead of useMemo.

const editorRef = useRef() if (!editorRef.current) editorRef.current = withReact(createEditor()); const editor = editorRef.current;ร

@mikestopcontinues You could try importing your module dynamically and disabling SSR

import dynamic from 'next/dynamic'

const DynamicComponentWithNoSSR = dynamic(
  () => import('../components/hello3'),
  { ssr: false }
)

How to fix it in CRA-driven project?