Subscribe on changes!

Uncaught (in promise): parentComponent.ctx.deactivate is not a function

avatar
Nov 7th 2023

Since calling defineComponent generates a new component with each update, causing isSameVNodeType to always return false, triggering an unmount, you should always use the same component for rendering.

demo

avatar
Nov 8th 2023

Since calling defineComponent generates a new component with each update, causing isSameVNodeType to always return false, triggering an unmount, you should always use the same component for rendering.由于调用 defineComponent 会在每次更新时生成一个新组件,导致 isSameVNodeType 始终返回 false,从而触发卸载,因此应始终使用相同的组件进行渲染。

demo

What if dynamically rendered components are required?

avatar
Nov 8th 2023

Dynamically rendered components must be cached as different instances of the same component. If a different component is called each time, it will not be cached.

avatar
Nov 8th 2023

Please note that in the isSameVNodeType function, the condition for determining the same component type is that the type of the two components is the same, and the key of the two components is also the same. However, in your example, each invocation of render returns different components, so they will be unmounted.

https://github.com/vuejs/core/blob/2c0414fdca5339857098b1eeaf2c74541f1451e3/packages/runtime-core/src/vnode.ts#L355-L369

avatar
Nov 8th 2023

Dynamically rendered components must be cached as different instances of the same component. If a different component is called each time, it will not be cached. must be cached as different instances of the same component It is no longer a dynamic component

avatar
Nov 8th 2023

我的问题,原意应该是动态渲染不同组件,A1 可缓存,B1 不缓存

demo

const render = (name) => defineComponent(() => () => h('main', h(Comp)), {name})
<component :is="render(active)" :key="active" />
avatar
Nov 14th 2023

const render = (name) => defineComponent(() => () => h('main', h(Comp)), {name})

我觉得这个写法不太对,每次都会调用 defineComponent 生成一个新的组件对象,所以不会被缓存,可以用闭包做一个函数缓存实现你想要的功能