# React node view loses selection when switching between block types

**URL:** <https://discuss.prosemirror.net/t/react-node-view-loses-selection-when-switching-between-block-types/4691>\
**Category:** Uncategorized\
**Created:** [June 19, 2022, 1:34pm UTC](https://discuss.prosemirror.net/t/react-node-view-loses-selection-when-switching-between-block-types/4691 "2022-06-19T13:34:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![isaacaderogba](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/isaacaderogba/32/2895_2.png) [@isaacaderogba](https://discuss.prosemirror.net/u/isaacaderogba)\
**Post date:** [June 19, 2022, 1:34pm UTC](https://discuss.prosemirror.net/t/react-node-view-loses-selection-when-switching-between-block-types/4691/1 "2022-06-19T13:34:57Z")

</div>

Using Portals seems to be the preferred approach for rendering custom node views in React. This works for the majority of cases, but I’m getting a weird bug where the cursor loses selection when converting from different block types.

Created a reproduction where converting from a heading block to the paragraph node view resets the selection to the start of the paragraph node view: [blissful-hamilton-n7gfin - CodeSandbox](https://codesandbox.io/s/blissful-hamilton-n7gfin?file=/src/Editor.js)

[![](https://img.youtube.com/vi/0C5ZkIgl4kY/maxresdefault.jpg "React node view loses selection when switching between block types") ](https://www.youtube.com/watch?v=0C5ZkIgl4kY)

[Asynchronous DOM mutation and CustomNodeView ignoreMutation](https://discuss.prosemirror.net/t/asynchronous-dom-mutation-and-customnodeview-ignoremutation/3588) makes me think that this may be an issue with async dom mutations, but I’m wondering if anyone else has been able to work around this?

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [June 19, 2022, 3:46pm UTC](https://discuss.prosemirror.net/t/react-node-view-loses-selection-when-switching-between-block-types/4691/2 "2022-06-19T15:46:16Z")

</div>

Are you using React to render a heading? Nodes will be recreated in the DOM when their type changes. Maybe that’s what you’re seeing?

---

<div class="post-metadata">

**Author:** ![isaacaderogba](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/isaacaderogba/32/2895_2.png) [@isaacaderogba](https://discuss.prosemirror.net/u/isaacaderogba)\
**Post date:** [June 19, 2022, 4:23pm UTC](https://discuss.prosemirror.net/t/react-node-view-loses-selection-when-switching-between-block-types/4691/3 "2022-06-19T16:23:51Z")

</div>

No, I’m using React to render the paragraph.

```auto
const view = new EditorView(
      { mount: ref.current },
      {
        state,
        nodeViews: {
          paragraph(node) {
            return new ParagraphView(node);
          }
        }
      }
    );

```

The heading is using the `toDOM` setup from `prosemirror-schema-basic`. And yes, I can confirm that the node is being recreated, though I’m surprised that the selection is being lost when this happens. Changing the block type on the editor from the Prosemirror [homepage](https://prosemirror.net/) retains the selection, for example.

I’m also including an `ignoreMutation` method that returns false for selection events, to leave Prosemirror in charge of managing the selection.

```auto
ignoreMutation(mutation) {
        if (mutation.type === "selection") return false;
        return !this.contentDOM.contains(mutation.target);
      }

```

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [June 19, 2022, 5:11pm UTC](https://discuss.prosemirror.net/t/react-node-view-loses-selection-when-switching-between-block-types/4691/4 "2022-06-19T17:11:13Z")

</div>

You probably don’t want to make rendering of nodes with content asynchronous—I can imagine that prevents ProseMirror from maintaining the DOM selection in them.
