@@ -14,7 +14,11 @@ import { warn } from './warning'
1414import { isRef , toRaw } from '@vue/reactivity'
1515import { ErrorCodes , callWithErrorHandling } from './errorHandling'
1616import { queuePostRenderEffect } from './renderer'
17- import { type ComponentOptions , getComponentPublicInstance } from './component'
17+ import {
18+ type ComponentOptions ,
19+ type Data ,
20+ getComponentPublicInstance ,
21+ } from './component'
1822import { knownTemplateRefs } from './helpers/useTemplateRef'
1923
2024/**
@@ -73,25 +77,7 @@ export function setRef(
7377 const oldRef = oldRawRef && ( oldRawRef as VNodeNormalizedRefAtom ) . r
7478 const refs = owner . refs === EMPTY_OBJ ? ( owner . refs = { } ) : owner . refs
7579 const setupState = owner . setupState
76- const rawSetupState = toRaw ( setupState )
77- const canSetSetupRef =
78- setupState === EMPTY_OBJ
79- ? ( ) => false
80- : ( key : string ) => {
81- if ( __DEV__ ) {
82- if ( hasOwn ( rawSetupState , key ) && ! isRef ( rawSetupState [ key ] ) ) {
83- warn (
84- `Template ref "${ key } " used on a non-ref value. ` +
85- `It will not work in the production build.` ,
86- )
87- }
88-
89- if ( knownTemplateRefs . has ( rawSetupState [ key ] as any ) ) {
90- return false
91- }
92- }
93- return hasOwn ( rawSetupState , key )
94- }
80+ const canSetSetupRef = createCanSetSetupRefChecker ( setupState )
9581
9682 // dynamic ref changed. unset old ref
9783 if ( oldRef != null && oldRef !== ref ) {
@@ -161,3 +147,26 @@ export function setRef(
161147 }
162148 }
163149}
150+
151+ export function createCanSetSetupRefChecker (
152+ setupState : Data ,
153+ ) : ( key : string ) => boolean {
154+ const rawSetupState = toRaw ( setupState )
155+ return setupState === EMPTY_OBJ
156+ ? ( ) => false
157+ : ( key : string ) => {
158+ if ( __DEV__ ) {
159+ if ( hasOwn ( rawSetupState , key ) && ! isRef ( rawSetupState [ key ] ) ) {
160+ warn (
161+ `Template ref "${ key } " used on a non-ref value. ` +
162+ `It will not work in the production build.` ,
163+ )
164+ }
165+
166+ if ( knownTemplateRefs . has ( rawSetupState [ key ] as any ) ) {
167+ return false
168+ }
169+ }
170+ return hasOwn ( rawSetupState , key )
171+ }
172+ }
0 commit comments