-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
fix(reactivity): throw an error during computed self-referencing #9920
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
Conversation
I'm skeptical that this use case should be handled, I don't think computed is designed to be used for recursive calculations, and Even if the special case handles self-referencing, it still doesn't solve the more obscure similar use cases, e.g.: const c = computed(() => d.value)
const d = computed(() => c.value) I think we should just ask the user not to do this. |
I prefer to throw an error for recursive calculations. To catch the above mentioned cases where c and d are accessing each other, or even more complex cases, get value() {
// the computed ref may get wrapped by other proxies e.g. readonly() #3376
const self = toRaw(this)
+ if (self.effect._runnings) {
+ throw new Error(`recursive calculations`)
+ }
if (!self._cacheable || self.effect.dirty) {
if (hasChanged(self._value, (self._value = self.effect.run()!))) {
triggerRefValue(self, DirtyLevels.ComputedValueDirty)
}
}
trackRefValue(self)
return self._value
} |
I agree this isn't a realistic use case and the user should never do this. An error is more appropriate. |
@johnsoncodehk done~ |
Should this PR be updated to point at the |
the problem is fixed via https://github.com/vuejs/core/pull/10101/files |
Closing as stale |
simply demo
make sure switch to version 3.4 DEV mode.(PROD mode causes the browser to crash)