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

modelValue doesn't update value by Array.push if using assigning #10318

Closed
ItMaga opened this issue Feb 11, 2024 · 3 comments
Closed

modelValue doesn't update value by Array.push if using assigning #10318

ItMaga opened this issue Feb 11, 2024 · 3 comments

Comments

@ItMaga
Copy link
Author

ItMaga commented Feb 11, 2024

The reason is that the computed get only updates on the next tick, after the parent has received the value, and re-rendered. So value.value.push(2) updates the old, empty array

I see how that can be counter-intuitive, but it's working as expected, from a technical perspective.

Solution: first, manipulate you array, then, assign the final result to the computed to trigger the emit.

https://play.vuejs.org/#eNqNVEtv2kAQ/isrXzASeEXpoaKASCMO7aGN2qoXnINjD+DE+9A+gMryf+/srg0OSdrePM/vm2/GW0c3UiYHC9EsmutcldIQDcbKZcpLJoUypCYKtqQhWyUYGWDq4By6FUy2/oQ6w3UafEx5ynPBtSFM78jC1ceb+yH65zRgYHc0DDBZZQbQImTumx3GTBRQLdIIS9OIUIzNaS8xGkUBfMwymTxqwZF57RqkbUCn0Yx4j/MhIWen0d4YqWeU5gXHMsQoDyrhYCiXjK4wjSrLTclgXAi2mibvk8kHWpTa9P0JaDZ+UOKoQWGXNBr1cCg6D6DGCngBCtT/4l6VPcO+ir3Ad/BNyhuUxWjUfFvurkTJUdayAvVNmhJ38kycrKrE8Yv3GWXhPEy+h/zpFf+jPoWh7hR4Zj0BTKZ2YEJ4/eMrnPD7HMSl2qpdxBvB76BFZR3HkPbJ8gJp9/I8289+xyXf/dTrkwGuu6EcUa+Gz/cLcRf11ugXutNk2lOxu+K//Q9OUmuguPopuqOXSkiNZ1/AtuRw56y4Jv6uf2WVhRm5USr7TZphVwGsNOeCNRo63gysLPDkZ5e6gf+FQsXBObCkoxL7wXCieNjOqJC14oFMcmmCHTqNcK7Y9+lKPI/4FeBRwHPwobi5MBH8tirzJ+SC0ItlaBVCHI6+HmObyb3zd55EWr2P36EAJLROuoG6DMT4x3uxnyzruhWiaeYUbe9/sMYITla5o4UPSUswjZb+rOc0JLx4WJo/1wS4ow==

Thank you for your quick response!

Did I understand correctly that this behaviour only works when using array methods along with assignment?

An example

value.value.push(1);
value.value.push(2);

and

value.value = [1];
value.value.push(2);

have different results

@LinusBorg
Copy link
Member

They have different results because only in the second example so you add a new array. In the first example, you manipulate the original array.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants