core: Incorrect template ref

Version

3.0.4

Reproduction link

https://codesandbox.io/s/misty-tree-xzmh1?file=/src/main.js

Steps to reproduce

Open the link, check the console

What is expected?

The template ref should be <div>hello</div>

What is actually happening?

The template ref is <div>foo</div>

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Is this because the <div> is being reused across renderings and the console logging shows the current content of that <div>, not the content it had when it was logged? The content of that <div> gets updated when it re-renders. You’d need to give it a key for the diffing to pair up the correct nodes.

Using console.log(refDom.value.innerHTML); seems to give the correct value.

I think this is a bug, if you replace patchKeyedChildren with patchUnkeyedChildren, then everything will work fine.

Check this link, the click event will be bound to div(foo): https://codesandbox.io/s/throbbing-snowflake-vpm1x, but when mounted, patch has not been performed yet, let alone reuse, the div(foo) doesn’t exist yet, right?

In this reproduction, if you remove the refVal.value && h('span', 'bar') sentence, it will also work correctly, I tracked this issue and its update process is as follows:

The pre-vnode:

[
  prev-comment1,
  prev-div(hello),
  prev-comment2
]

The next-vnode:

[
  next-div(foo),
  next-div(hello),
  next-div(bar)
]
    1. remove the prev-comment1
    1. patch: prev-div(hello) ---> next-div(foo)
    1. remove the comment2
    1. create: next div(bar)
    1. create: next div(hello)

But I don’t know what caused the problem, it’s weird, just like @edison1105 said.

Your solution is not correct. There is no problem in reusing nodes, because pre-div(hello) and next-div(foo) should be reusable, because their type(div) and key(undefined) are the same, which has nothing to do with ref dom.