From 5f4d4134d102c2aa01bc9472f70d640ef61dc1a7 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 7 Feb 2024 13:24:03 +0800 Subject: [PATCH] revert: perf(templateRef): avoid double render when using template ref on v-for This reverts commit de4d2e2143ea8397cebeb1c7a57a60007b283c9f. reopen #9908 close #10210, close #10234 --- packages/runtime-core/src/rendererTemplateRef.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/runtime-core/src/rendererTemplateRef.ts b/packages/runtime-core/src/rendererTemplateRef.ts index 7f991bc9f5f..b652edeac4c 100644 --- a/packages/runtime-core/src/rendererTemplateRef.ts +++ b/packages/runtime-core/src/rendererTemplateRef.ts @@ -81,10 +81,9 @@ export function setRef( } else { const _isString = isString(ref) const _isRef = isRef(ref) - const isVFor = rawRef.f if (_isString || _isRef) { const doSet = () => { - if (isVFor) { + if (rawRef.f) { const existing = _isString ? hasOwn(setupState, ref) ? setupState[ref] @@ -119,15 +118,14 @@ export function setRef( warn('Invalid template ref type:', ref, `(${typeof ref})`) } } - // #9908 ref on v-for mutates the same array for both mount and unmount - // and should be done together - if (isUnmount || isVFor) { - doSet() - } else { - // #1789: set new refs in a post job so that they don't get overwritten - // by unmounting ones. + if (value) { + // #1789: for non-null values, set them after render + // null values means this is unmount and it should not overwrite another + // ref with the same key ;(doSet as SchedulerJob).id = -1 queuePostRenderEffect(doSet, parentSuspense) + } else { + doSet() } } else if (__DEV__) { warn('Invalid template ref type:', ref, `(${typeof ref})`)