MCPcopy
hub / github.com/vuejs/core / setRef

Function setRef

packages/runtime-core/src/rendererTemplateRef.ts:31–212  ·  view source on GitHub ↗
(
  rawRef: VNodeNormalizedRef,
  oldRawRef: VNodeNormalizedRef | null,
  parentSuspense: SuspenseBoundary | null,
  vnode: VNode,
  isUnmount = false,
)

Source from the content-addressed store, hash-verified

29 * Function for handling a template ref
30 */
31export function setRef(
32 rawRef: VNodeNormalizedRef,
33 oldRawRef: VNodeNormalizedRef | null,
34 parentSuspense: SuspenseBoundary | null,
35 vnode: VNode,
36 isUnmount = false,
37): void {
38 if (isArray(rawRef)) {
39 rawRef.forEach((r, i) =>
40 setRef(
41 r,
42 oldRawRef && (isArray(oldRawRef) ? oldRawRef[i] : oldRawRef),
43 parentSuspense,
44 vnode,
45 isUnmount,
46 ),
47 )
48 return
49 }
50
51 if (isAsyncWrapper(vnode) && !isUnmount) {
52 // #4999 if an async component already resolved and cached by KeepAlive,
53 // we need to set the ref to inner component
54 if (
55 vnode.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE &&
56 (vnode.type as ComponentOptions).__asyncResolved &&
57 vnode.component!.subTree.component
58 ) {
59 setRef(rawRef, oldRawRef, parentSuspense, vnode.component!.subTree)
60 }
61
62 // otherwise, nothing needs to be done because the template ref
63 // is forwarded to inner component
64 return
65 }
66
67 const refValue =
68 vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT
69 ? getComponentPublicInstance(vnode.component!)
70 : vnode.el
71 const value = isUnmount ? null : refValue
72
73 const { i: owner, r: ref } = rawRef
74 if (__DEV__ && !owner) {
75 warn(
76 `Missing ref owner context. ref cannot be used on hoisted vnodes. ` +
77 `A vnode with ref must be created inside the render function.`,
78 )
79 return
80 }
81 const oldRef = oldRawRef && (oldRawRef as VNodeNormalizedRefAtom).r
82 const refs = owner.refs === EMPTY_OBJ ? (owner.refs = {}) : owner.refs
83 const setupState = owner.setupState
84 const rawSetupState = toRaw(setupState)
85 const canSetSetupRef =
86 setupState === EMPTY_OBJ
87 ? NO
88 : (key: string) => {

Callers 3

patchFunction · 0.90
unmountFunction · 0.90
hydrateNodeFunction · 0.90

Calls 15

isAsyncWrapperFunction · 0.90
warnFunction · 0.90
toRawFunction · 0.90
hasOwnFunction · 0.90
isRefFunction · 0.90
isTemplateRefKeyFunction · 0.90
isStringFunction · 0.90
isFunctionFunction · 0.90
pauseTrackingFunction · 0.90
callWithErrorHandlingFunction · 0.90
resetTrackingFunction · 0.90

Tested by

no test coverage detected