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):v-for ref behaves differently under prod and dev #6697 #6714

Merged
merged 9 commits into from Oct 14, 2022

Conversation

baiwusanyu-c
Copy link
Member

@baiwusanyu-c baiwusanyu-c commented Sep 21, 2022

close: #6697

@baiwusanyu-c
Copy link
Member Author

baiwusanyu-c commented Sep 25, 2022

wdyt? @yyx990803 😂

Copy link
Member

@LinusBorg LinusBorg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better / correct solution would involve reading ref.value in lines 101/102 in order to make it a template dependency in DEV as well (where the ref is only passed via it's string name and thus not being read in line 87).

@@ -61,7 +61,8 @@ export function setRef(
return
}
const oldRef = oldRawRef && (oldRawRef as VNodeNormalizedRefAtom).r
const refs = owner.refs === EMPTY_OBJ ? (owner.refs = {}) : owner.refs
const refs =
owner.refs === EMPTY_OBJ ? (owner.refs = reactive({})) : owner.refs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right fix. "Options API" refs haven't been reactive in Vue 2 or Vue 3. The only way to get a reactive ref is through using a an actual ref()/reactive() object like in the reported issue.

@baiwusanyu-c
Copy link
Member Author

A better / correct solution would involve reading ref.value in lines 101/102 in order to make it a template dependency in DEV as well (where the ref is only passed via it's string name and thus not being read in line 87).

 } else if (!existing.includes(refValue)) {
              existing.push(refValue)
            }

Do you mean these two lines? Lines 101~102

@yyx990803
Copy link
Member

The fix is close - the root cause is that in unmount / already array branches, we are only mutating the refs array, however the array is non-reactive when we get it from the refs object.

When in dev mode or the binding ref is returned from setup(), the existing value should be retrieved from setupState so that it's reactive.

I made the small change to reflect this logic and reused the test.

@yyx990803 yyx990803 merged commit 9ae796d into vuejs:main Oct 14, 2022
@baiwusanyu-c baiwusanyu-c deleted the bwsy/fix/renderRef branch October 29, 2022 06:41
chrislone pushed a commit to chrislone/core that referenced this pull request Feb 4, 2023
zhangzhonghe pushed a commit to zhangzhonghe/core that referenced this pull request Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

v-for ref behaves differently under production and development
4 participants