Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): setRef should not update TemplateRef #11804

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ export function handleSetupResult(
instance.devtoolsRawSetupState = setupResult
}
instance.setupState = proxyRefs(setupResult)
instance.setupState.__v__setupResult = setupResult
if (__DEV__) {
exposeSetupStateOnRenderContext(instance)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/helpers/useTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export function useTemplateRef<T = unknown, Keys extends string = string>(
): Readonly<ShallowRef<T | null>> {
const i = getCurrentInstance()
const r = shallowRef(null)
// @ts-expect-error
r.__v__TemplateRef = true
if (i) {
const refs = i.refs === EMPTY_OBJ ? (i.refs = {}) : i.refs

Expand Down
22 changes: 20 additions & 2 deletions packages/runtime-core/src/rendererTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@ export function setRef(
if (_isString) {
refs[ref] = [refValue]
if (hasOwn(setupState, ref)) {
setupState[ref] = refs[ref]
if (
// @ts-expect-error
setupState.__v__setupResult[ref] &&
// @ts-expect-error
setupState.__v__setupResult[ref].__v__TemplateRef
) {
// cannot update TemplateRef
} else {
setupState[ref] = value
}
}
} else {
ref.value = [refValue]
Expand All @@ -109,7 +118,16 @@ export function setRef(
} else if (_isString) {
refs[ref] = value
if (hasOwn(setupState, ref)) {
setupState[ref] = value
if (
// @ts-expect-error
setupState.__v__setupResult[ref] &&
// @ts-expect-error
setupState.__v__setupResult[ref].__v__TemplateRef
) {
// cannot update TemplateRef
} else {
setupState[ref] = value
}
}
} else if (_isRef) {
ref.value = value
Expand Down
Loading