Skip to content

Commit 10a46f4

Browse files
authored
chore(runtime-core): warn if use a non-ref to hold the element reference in DEV (#12051)
close #12029
1 parent d3ecde8 commit 10a46f4

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

packages/runtime-core/__tests__/rendererTemplateRef.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ describe('api: template refs', () => {
217217
}
218218
render(h(Comp), root)
219219
expect(state.refKey).toBe(root.children[0])
220+
expect('Template ref "refKey" used on a non-ref value').toHaveBeenWarned()
220221
})
221222

222223
test('multiple root refs', () => {

packages/runtime-core/src/rendererTemplateRef.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ export function setRef(
6969
setupState === EMPTY_OBJ
7070
? () => false
7171
: (key: string) => {
72-
if (__DEV__ && knownTemplateRefs.has(rawSetupState[key] as any)) {
73-
return false
72+
if (__DEV__) {
73+
if (hasOwn(rawSetupState, key) && !isRef(rawSetupState[key])) {
74+
warn(
75+
`Template ref "${key}" used on a non-ref value. ` +
76+
`It will not work in the production build.`,
77+
)
78+
}
79+
80+
if (knownTemplateRefs.has(rawSetupState[key] as any)) {
81+
return false
82+
}
7483
}
7584
return hasOwn(rawSetupState, key)
7685
}

0 commit comments

Comments
 (0)