Error when setting state with view.setProps

I have an initialization function to create a PM editor and which I rerun whenever editor props change. As part of this, I recreate the plugins which I reconfigure on the old state with state.reconfigure. Then I attempt to set the state with view.setProps but which crashes throwing:

Uncaught TypeError: Cannot read properties of null (reading 'matchesNode')
    at EditorView.updateStateInner

Here is a code sandbox sharp-khorana-mcpwz - CodeSandbox

But the main part is:

  function init(
    element: HTMLElement,
    props: EditorProps,
    oldView?: EditorView | null
  ) {
    if (oldView) {
      const state = oldView.state.reconfigure({
        plugins: exampleSetup({ schema })
      });
      oldView.setProps({
        state
      });
      return oldView;
    }
    const state = EditorState.create({
      schema,
      plugins: exampleSetup({ schema })
    });
    const view = new EditorView(
      { mount: element },
      {
        state
      }
    );
    props.onEditorReady && props.onEditorReady(view);
    return view;
  }

Looks like you’re trying to use an editor view after calling destroy on it.

Huh. Well can’t say React hooks haven’t thrown me off before. Thanks. Removing the view.destroy fixed it. Guess should have also set the ref to undefined to know the cause immediately.