Vue-next: Slot of <component> is cached

Created on 30 Aug 2020  路  4Comments  路  Source: vuejs/vue-next

Version

3.0.0-rc.9, 3.0.0-rc.6, 3.0.0-rc.1

Reproduction link

https://codepen.io/FrankFang/pen/jOqweRE?editors=0010

Steps to reproduce

click the button to set "selected" to "tab2"

What is expected?

"content 1" turns to "content 2"

What is actually happening?

"content 1" stays there

has workaround

Most helpful comment

When using <component :is="vnode"> and passing vnode of the same type, you need to provide keys:

<component :is="current" :key="selected" />

Otherwise, you are passing two compiled vnodes of the same type to the renderer. Because they are compiled as completely static, they will not be updated at all.

All 4 comments

Not completely sure of the usage of slots like that but this also fails:

setup(props, context){
    return () => {
      const defaults = context.slots.default()

      const current = defaults.filter((tag) => {
        return tag.props.title === props.selected
      })[0]

      console.log('tab', current)

      return h('div', {}, current)
    }
  }

Adding a key makes it work:

return h('div', {}, Vue.cloneVNode(current, { key: props.selected }))

Isn't it a bug?

I've updated the demo: https://codepen.io/FrankFang/pen/dyMzOBd?editors=0010
It's a simple tab switcher with a bug.

  1. click tab 1
  2. click tab 2

expected: content changes
fact: content never changes

When using <component :is="vnode"> and passing vnode of the same type, you need to provide keys:

<component :is="current" :key="selected" />

Otherwise, you are passing two compiled vnodes of the same type to the renderer. Because they are compiled as completely static, they will not be updated at all.

Was this page helpful?
0 / 5 - 0 ratings