Skip to content

Commit

Permalink
feat(templateRef): support template ref for all vnode types
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 31, 2020
1 parent 94562da commit 55b364d
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ function baseCreateRenderer(
n1 = null
}

const { type, shapeFlag } = n2
const { type, ref, shapeFlag } = n2
switch (type) {
case Text:
processText(n1, n2, container, anchor)
Expand Down Expand Up @@ -439,6 +439,13 @@ function baseCreateRenderer(
warn('Invalid VNode type:', type, `(${typeof type})`)
}
}

// set ref
if (ref != null && parentComponent) {
const refValue =
shapeFlag & ShapeFlags.STATEFUL_COMPONENT ? n2.component!.proxy : n2.el
setRef(ref, n1 && n1.ref, parentComponent, refValue)
}
}

const processText: ProcessTextOrCommentFn = (n1, n2, container, anchor) => {
Expand Down Expand Up @@ -518,9 +525,6 @@ function baseCreateRenderer(
} else {
patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized)
}
if (n2.ref != null && parentComponent) {
setRef(n2.ref, n1 && n1.ref, parentComponent, n2.el)
}
}

const mountElement = (
Expand Down Expand Up @@ -1005,17 +1009,6 @@ function baseCreateRenderer(
n2.el = n1.el
}
}
if (n2.ref != null && parentComponent) {
if (__DEV__ && !(n2.shapeFlag & ShapeFlags.STATEFUL_COMPONENT)) {
pushWarningContext(n2)
warn(
`Functional components do not support "ref" because they do not ` +
`have instances.`
)
popWarningContext()
}
setRef(n2.ref, n1 && n1.ref, parentComponent, n2.component!.proxy)
}
}

const mountComponent: MountComponentFn = (
Expand Down

0 comments on commit 55b364d

Please sign in to comment.