[RFC] Returning onUnmounted within onMounted #593
Replies: 8 comments 10 replies
-
|
What's the motivation? Accessing state inside onMounted(() => {
console.log('onMounted')
onUnmounted(() => {
console.log('onUnmounted')
})
})If it works, what's the benefit of returning a function? And in both cases... I wonder: if a component is recycled (unmounted from DOM, then mounted again), both approaches would create multiple |
Beta Was this translation helpful? Give feedback.
-
|
This method is functionally consistent with |
Beta Was this translation helpful? Give feedback.
-
|
I feel this adds another way to do things without much gain. I can't just see if I've attached an If I only want to listen for unmount, then I must use a dedicated hook. And I agree with @jods4 about the timing: if a component is unmounted and mounted again, is a new unmount callback registered? What do you do with the existing one? |
Beta Was this translation helpful? Give feedback.
-
|
I understand the OP's point is that returning an
|
Beta Was this translation helpful? Give feedback.
-
|
I feel like this will just make Vue harder to learn and vue apps harder to maintain, because there will be 2 ways of doing the same thing. If there is nothing to gain from it, I would avoid increasing the complexity of the framework. |
Beta Was this translation helpful? Give feedback.
-
|
I think explicitly calling If we don't want to explicitly import onMounted((onUnmounted) => {
console.log("mounted")
onUnmounted(() => {
console.log("unmounted")
})
})I'd rather imported |
Beta Was this translation helpful? Give feedback.
-
|
Temporarily close this RFC. If someone has passed a function with a return value to |
Beta Was this translation helpful? Give feedback.
-
|
I'm a little late to the discussion, but I'd like to give some insights on actual use cases for this implementation. The "issue" has to do with variable scoping. Example where it would be very usfull: onMounted(() => {
const observer = new IntersectionObserver(
(entries) => {
// some logic here
},
);
observer.observe(el);
return () => {
observer.disconnect();
};
});This would be true for any client side logic that needs cleanup when unmounted. The extra overhead of the none scoped variable with extra typing and logic of checking if the variable is actually declared is very unergonomic. Here the above example with the corrent way of doing things: let observer: IntersectionObserver|undefined;
onMounted(() => {
observer = new IntersectionObserver(
(entries) => {
// some logic here
},
);
observer.observe(el);
});
onUnmounted(() => {
if(observer) observer.disconnect();
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Expecting to return a function in onMounted that will be called in onUnmounted.
PR
expected:
Beta Was this translation helpful? Give feedback.
All reactions