Skip to content
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

fix(n-cascader): 修复未正确移除事件导致的内存泄漏问题 #6313

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/_utils/composable/use-resize.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { onBeforeUnmount, onMounted, type Ref } from 'vue'
import { resizeObserverManager } from 'vueuc'

interface UseOnResizeOptions {
/**
* In some cases
* if a reactive variable is used in the render function to control whether or not the dom is rendered,
* the event cannot be cleared in onBeforeUnmount because the dom no longer exists,
* but the event contains a reference to the dom, resulting in a memory leak
*/
show?: Ref<boolean>
}
export function useOnResize(
elRef: Ref<HTMLElement | null>,
onResize: (() => void) | undefined
onResize: (() => void) | undefined,
options?: UseOnResizeOptions
): void {
// it needn't be reactive since it's for internal usage
if (onResize) {
Expand All @@ -19,5 +29,14 @@
resizeObserverManager.unregisterHandler(el)
}
})
if (options?.show && isRef(options.show)) {

Check failure on line 32 in src/_utils/composable/use-resize.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Cannot find name 'isRef'.

Check failure on line 32 in src/_utils/composable/use-resize.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Cannot find name 'isRef'.
onBeforeUpdate(() => {

Check failure on line 33 in src/_utils/composable/use-resize.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Cannot find name 'onBeforeUpdate'.

Check failure on line 33 in src/_utils/composable/use-resize.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Cannot find name 'onBeforeUpdate'.
const { value: el } = elRef
const { value: show } = options.show!
if (!show && el) {
resizeObserverManager.unregisterHandler(el)
}
})
}
}
}
3 changes: 2 additions & 1 deletion src/cascader/src/CascaderMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
inject,
type PropType,
ref,
toRef,
Transition,
withDirectives
} from 'vue'
Expand Down Expand Up @@ -74,7 +75,7 @@ export default defineComponent({
function handleResize(): void {
syncCascaderMenuPosition()
}
useOnResize(selfElRef, handleResize)
useOnResize(selfElRef, handleResize, { show: toRef(props, 'show') })
function showErrorMessage(label: string): void {
const {
value: { loadingRequiredMessage }
Expand Down
Loading