Should the parameter (currentValue) in the watch function be mutable? #13859
Replies: 2 comments 1 reply
-
So, maybe a eslint rule can solve this? |
Beta Was this translation helpful? Give feedback.
-
This appears to be working correctly to me. Watchers are allowed to trigger recursive updates. This isn't an accident, there's specific code to ensure it works: core/packages/runtime-core/src/apiWatch.ts Lines 212 to 216 in 75220c7 Recursively updating state like this is valid usage, and the callback is responsible for ensuring that the watched state eventually stabilises so it doesn't recurse indefinitely. Updating the property via I'm not sure what form immutability would take in this context, but I would also note that attempting to make it immutable by wrapping it in a proxy would break equality checks within the callback, so that isn't really a viable approach. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
consider we have a simple sample:
In this sample, the
values
will change twice, and the watch sideffectverify
will trigger twice. The second trigger is because, inside the watcher, we changed data['some-temp-metadata'].Is this correct behavior (i know we should avoid it, but it exists), or should it be banned by making currentValue immutable?
Beta Was this translation helpful? Give feedback.
All reactions