Skip to content

Commit 55b364d

Browse files
committed
feat(templateRef): support template ref for all vnode types
1 parent 94562da commit 55b364d

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

packages/runtime-core/src/renderer.ts

+8-15
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ function baseCreateRenderer(
363363
n1 = null
364364
}
365365

366-
const { type, shapeFlag } = n2
366+
const { type, ref, shapeFlag } = n2
367367
switch (type) {
368368
case Text:
369369
processText(n1, n2, container, anchor)
@@ -439,6 +439,13 @@ function baseCreateRenderer(
439439
warn('Invalid VNode type:', type, `(${typeof type})`)
440440
}
441441
}
442+
443+
// set ref
444+
if (ref != null && parentComponent) {
445+
const refValue =
446+
shapeFlag & ShapeFlags.STATEFUL_COMPONENT ? n2.component!.proxy : n2.el
447+
setRef(ref, n1 && n1.ref, parentComponent, refValue)
448+
}
442449
}
443450

444451
const processText: ProcessTextOrCommentFn = (n1, n2, container, anchor) => {
@@ -518,9 +525,6 @@ function baseCreateRenderer(
518525
} else {
519526
patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized)
520527
}
521-
if (n2.ref != null && parentComponent) {
522-
setRef(n2.ref, n1 && n1.ref, parentComponent, n2.el)
523-
}
524528
}
525529

526530
const mountElement = (
@@ -1005,17 +1009,6 @@ function baseCreateRenderer(
10051009
n2.el = n1.el
10061010
}
10071011
}
1008-
if (n2.ref != null && parentComponent) {
1009-
if (__DEV__ && !(n2.shapeFlag & ShapeFlags.STATEFUL_COMPONENT)) {
1010-
pushWarningContext(n2)
1011-
warn(
1012-
`Functional components do not support "ref" because they do not ` +
1013-
`have instances.`
1014-
)
1015-
popWarningContext()
1016-
}
1017-
setRef(n2.ref, n1 && n1.ref, parentComponent, n2.component!.proxy)
1018-
}
10191012
}
10201013

10211014
const mountComponent: MountComponentFn = (

0 commit comments

Comments
 (0)