computed value becomes `undefined` inside `onUnmounted` hook unless it's accessed before
Version
3.0.5
Reproduction link
https://codesandbox.io/s/priceless-lalande-eh4wx?file=/src/views/Home.vue
Steps to reproduce
- Go to Codesandbox.
- Open console window in Codesandbox.
- First access "Home" page, then move to "About" page.
- You'll see computed value becoming
undefined
. - If you un-comment
console.log(computedObj.value)
right above theonUnmounted
hook. The value becomes visible insideonMountedhook
.
What is expected?
Not sure. Is this expected?
What is actually happening?
Not sure. I think it would be OK for computed
or any other reactive value to be undefined
inside onUnmounted
, but I think it would be better to have consistent behavior.
in the unmounted
stage, effects have been stopped, and stopped effects (if using a scheduler) return undefined
:
In that sense, it's expected.
But if we don't want this behaviour for computed
, we could implement a check for effect.active
in computed's implementation, and run effect.raw()
for stopped computeds, around here:
The downside would be that the value would no longer be cached, but at least consumers should still be able to get the value.
Yeah that's true. Getting back cached computed value sounds right. I think this is tiny issue not sure if it's worth doing something about it, but I can understand it could be confusing because if the computed is accessed once, it looks like computed
is working inside unmounted
hook... maybe we should document it as an edge case...?