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

triggerRef fails when target is an array #12427

Open
ilyary opened this issue Nov 18, 2024 · 4 comments · May be fixed by #12431
Open

triggerRef fails when target is an array #12427

ilyary opened this issue Nov 18, 2024 · 4 comments · May be fixed by #12431
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. has workaround A workaround has been found to avoid the problem scope: reactivity

Comments

@ilyary
Copy link

ilyary commented Nov 18, 2024

Vue version

3.5.6

Link to minimal reproduction

https://play.vuejs.org/#eNqVU8tu2zAQ/JUtL5IBVW7QnlzZSFsYaHtIi6RAD2UPsrySlVIkwYdiQNC/d0klthMEDnIRxJnZ4SyXHNgnrfPeI1uwwlam1Q4sOq9XXLadVsbBAAbrjD5l5doeM3DqOgB3pat267rGyhFm2qZBQ8QItVEdJOSZcMllpaR10NkGlsEoTb6iEAp+KyO2b5LZUVIr00XNtE86gLdoFrQ/dmUrFvAnKS/LvFJdkkGyudzE378wwjj7eLSJ4ivfbWIaMoxx0+CeB8M8CjJ4F4pOekjTGSxXMHAJEJyUwFyoJn3sl/el8BhK7zc9Nv5EOdGPUt2QWDZnUyUXyWtyHRxfzHVQBrqYT6OmIdPCYadF6ZBWAMXuYjUMcV7jWMxpFdFWau+gf9upLYolZ8RzBnMii/lJPcuYs5Sybpv81ipJtyoG54xmpVuB5od2LXXBGQ02MIEr6ULcfY+YMx6zB7zaYfXvGfzW7gPG2U+DdHY9cnbgXGkadBO9vrnCPf0fSErvBanPkNdIJ+xDxkn22cstxT7RxbTf4tugE/1l13uH0j40FYIG5Rj1nNEz+HKm9WPc9/mHWEcTZON/Pog4cQ==

Steps to reproduce

const form = reactive({ user: { email: ['[email protected]', '[email protected]'] } });

const emailNumberRef = toRef(form.user.email, 0);
watchEffect(() => {
  console.log(emailNumberRef.value);
});

triggerRef(emailNumberRef); // <-- FAILS
/*
@vue/reactivity/dist/reactivity.cjs.js:1487
    ref2.dep.trigger({
             ^

TypeError: Cannot read properties of undefined (reading 'trigger')
*/

const emailStringRef = toRef(form.user.email, '1'); // <-- TS is not happy
watchEffect(() => {
  console.log(emailStringRef.value);
});

triggerRef(emailStringRef); // <-- WORKS FINE

On The SFC playground it fails silently. No effect is called for number prop, but it works fine for string prop

What is expected?

triggerRef triggers effects for a reference with an integer key

What is actually happening?

triggerRef fails on a reference with an integer key

System Info

System:
    OS: macOS 14.4.1
    CPU: (8) arm64 Apple M1
    Memory: 66.09 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.2 - ~/.nvm/versions/node/v20.12.2/bin/node
    npm: 10.5.0 - ~/.nvm/versions/node/v20.12.2/bin/npm
    pnpm: 9.10.0 - ~/.nvm/versions/node/v20.12.2/bin/pnpm
  Browsers:
    Chrome: 130.0.6723.117
    Safari: 17.4.1

Any additional comments?

it happens because Proxy index gets converted to string.

So, here

track(target, TrackOpTypes.GET, key)
the property starts being tracked with a string key but later on is referenced with a number key
const depMap = targetMap.get(object)

Consider converting string key to number for arrays when tracking

@edison1105
Copy link
Member

edison1105 commented Nov 18, 2024

It works fine with v3.5.13, see Playground
I think only the ts error should be fixed.

@ilyary
Copy link
Author

ilyary commented Nov 18, 2024

@edison1105, no, it's not

You may observe in the console one output from number and two outputs from string. it should be two outputs from number as well. it just fails silently((

[@vue/repl] successfully compiled 1 module.
VM51 about:srcdoc:159 number [email protected]
VM51 about:srcdoc:159 [email protected]
VM51 about:srcdoc:159 [email protected]

Pls, review again

@ilyary
Copy link
Author

ilyary commented Nov 18, 2024

@vue/reactivity/dist/reactivity.cjs.js:1487
    ref2.dep.trigger({
             ^

TypeError: Cannot read properties of undefined (reading 'trigger')

this error is printed in Node.js , when executing this piece of code in shell

Browser is silent

@ilyary
Copy link
Author

ilyary commented Nov 18, 2024

it's not only TS.

the basic cause is how Proxy treats properties e.g. Proxy index gets converted to string

@edison1105 edison1105 added scope: reactivity has workaround A workaround has been found to avoid the problem 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. and removed scope: types labels Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. has workaround A workaround has been found to avoid the problem scope: reactivity
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants