2024-07-31 21:42:55 +02:00
|
|
|
/**
|
|
|
|
|
* Creates a computed reference that uses a provide getter function called with an argument representing the current mount state of the component.
|
|
|
|
|
* @param getter A getter function that will run with `mounted` argument representing whether or not the component is mounted.
|
|
|
|
|
* @returns A computed reference that changes when component becomes mounted or unmounted.
|
|
|
|
|
*/
|
|
|
|
|
export function useMountedValue<T>(getter: (isMounted: boolean) => T) {
|
2025-08-14 21:48:38 +01:00
|
|
|
const mounted = ref(getCurrentInstance()?.isMounted ?? false)
|
2024-07-31 21:42:55 +02:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
onMounted(() => (mounted.value = true))
|
2024-07-31 21:42:55 +02:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
onUnmounted(() => (mounted.value = false))
|
2024-07-31 21:42:55 +02:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
return computed(() => getter(mounted.value))
|
2024-07-31 21:42:55 +02:00
|
|
|
}
|