Skip to content

Commit

Permalink
chore(runtime-core): warn if use a non-ref to hold the element refere…
Browse files Browse the repository at this point in the history
…nce in DEV (#12051)

close #12029
  • Loading branch information
edison1105 authored Oct 11, 2024
1 parent d3ecde8 commit 10a46f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ describe('api: template refs', () => {
}
render(h(Comp), root)
expect(state.refKey).toBe(root.children[0])
expect('Template ref "refKey" used on a non-ref value').toHaveBeenWarned()
})

test('multiple root refs', () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ export function setRef(
setupState === EMPTY_OBJ
? () => false
: (key: string) => {
if (__DEV__ && knownTemplateRefs.has(rawSetupState[key] as any)) {
return false
if (__DEV__) {
if (hasOwn(rawSetupState, key) && !isRef(rawSetupState[key])) {
warn(
`Template ref "${key}" used on a non-ref value. ` +
`It will not work in the production build.`,
)
}

if (knownTemplateRefs.has(rawSetupState[key] as any)) {
return false
}
}
return hasOwn(rawSetupState, key)
}
Expand Down

0 comments on commit 10a46f4

Please sign in to comment.