# Is there a way to get a Node View from a Node object?

**URL:** https://discuss.prosemirror.net/t/is-there-a-way-to-get-a-node-view-from-a-node-object/1403
**Category:** Uncategorized
**Created:** [June 26, 2018, 1:01am UTC](https://discuss.prosemirror.net/t/is-there-a-way-to-get-a-node-view-from-a-node-object/1403 "2018-06-26T01:01:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jazmine](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jazmine](https://discuss.prosemirror.net/u/jazmine)
#### Post date: [June 26, 2018, 1:01am UTC](https://discuss.prosemirror.net/t/is-there-a-way-to-get-a-node-view-from-a-node-object/1403/1 "2018-06-26T01:01:32Z")

</div>

I created a custom Node View for one of my nodes and have it updating the view using Steps similar to how jstleger0 described [here](https://discuss.prosemirror.net/t/changing-doc-attrs/784/16) (except using node.attrs[key] instead of doc.attrs[key]) whenever the content of the node changes. Essentially I’ll pass in the node and node view to the Step then update one of the nodes attributes and rerender the node view.

I noticed that when I undo long enough to the point that the node is removed from dom then redo that node back into existence, a new Node gets constructed with a new Node View (I assume) because continuing to redo causes the node attrs to update but any commands I run on the Node View are not visible. Using console.log, I can see the Node View still exists in memory but it is not the same Node View.

I think the solution might be either to get the corresponding Node View from the Node and use that to update the view… am I on to something?..

---

<div class="post-metadata">

### Author: ![jazmine](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jazmine](https://discuss.prosemirror.net/u/jazmine)
#### Post date: [June 26, 2018, 2:41am UTC](https://discuss.prosemirror.net/t/is-there-a-way-to-get-a-node-view-from-a-node-object/1403/2 "2018-06-26T02:41:14Z")

</div>

More problems 😩… If I add a new node of the same type after already adding and interacting with the old one, the new one’s attrs are created and set the same as the previous one. Looks like using the Step method I described before sets the attr default somehow 😭😭😭

---

<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 26, 2018, 6:53am UTC](https://discuss.prosemirror.net/t/is-there-a-way-to-get-a-node-view-from-a-node-object/1403/3 "2018-06-26T06:53:23Z")

</div>

> [@jazmine](#):
>
> I think the solution might be either to get the corresponding Node View from the Node and use that to update the view… am I on to something?..

I don’t think there is a problem—node views are created on demand to display nodes, but there is no guarantee that a given node view remains tied to a given node. In fact, nodes shouldn’t be treated as having identity at all—they just describe a given piece of document structure, but their object identity isn’t relevant. A single node object might appear in the document multiple times, for example, and replacing a node with another instance of the same structure will not cause the DOM to change at all.

---

<div class="post-metadata">

### Author: ![jazmine](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jazmine](https://discuss.prosemirror.net/u/jazmine)
#### Post date: [June 27, 2018, 1:24am UTC](https://discuss.prosemirror.net/t/is-there-a-way-to-get-a-node-view-from-a-node-object/1403/5 "2018-06-27T01:24:41Z")

</div>

How do I set and replace a text node as a child into my custom node/node view?

```
let tr = this.view.state.tr;
tr.insertText("hello!", this.getPos()+1);
this.view.dispatch(tr);

```

That’s as far as I’ve gotten but it only appends text. I can’t make sense of how to select and replace it once it’s there…

---

<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 27, 2018, 6:37am UTC](https://discuss.prosemirror.net/t/is-there-a-way-to-get-a-node-view-from-a-node-object/1403/6 "2018-06-27T06:37:31Z")

</div>

If you track your node view’s content node (keeping it in a property and updating it when the node view’s `update` method is called), you can use that to determine the node size and do something like `tr.replace(this.getPos() + 1, this.getPos() + 1 + this.node.content.size, schema.text("new content"))`. (Or you can query `this.view.state.doc` for the node at `getPos()` to find the current node.)

---

<div class="post-metadata">

### Author: ![jazmine](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jazmine](https://discuss.prosemirror.net/u/jazmine)
#### Post date: [June 28, 2018, 6:37pm UTC](https://discuss.prosemirror.net/t/is-there-a-way-to-get-a-node-view-from-a-node-object/1403/7 "2018-06-28T18:37:01Z")

</div>

Thank you! That really got me further along to a solution! I tried using tr.replace() just like you mentioned but it didn’t seem to work. It complained something about the open sides not fitting. Does this mean the new text content must be the same length as the one I’m replacing?

Taking inspiration from your response, I decided to do this instead:

```
        let innerTextSelection = TextSelection.create(
            this.view.docView.node,
            this.getPos()+1,
            this.getPos()+1+this.node.content.size                
        );
        tr.setSelection(
            innerTextSelection
        ).replaceSelectionWith(
            schema.text('new text!'),
            false   
        );

```

Weirdly enough though, That didn’t work either ☹ however, it did have the handy side effect of deleting the inner text that was there before. So for now I just tacked on a `tr.insertText("new text!", this.getPos()+1);` after that to place my new text in. It’s hacky, I know 😅😅

I can’t seem to figure out why replaceSelectionWith() isn’t working. `schema.text()` returns a valid text node and the fact that when I call it, the inner text content of my node is removed seems to imply that I made the right selection. thoughts?
