From 3368da43edb8c7b43dab6e112e6f975a8ad30a00 Mon Sep 17 00:00:00 2001 From: iAmBimo Date: Wed, 1 Jan 2025 19:04:12 +0800 Subject: [PATCH] feat: usemount support destructor --- .../hooks/src/useMount/__tests__/index.test.ts | 14 ++++++++++---- packages/hooks/src/useMount/demo/demo1.tsx | 6 +++++- packages/hooks/src/useMount/index.en-US.md | 4 ++-- packages/hooks/src/useMount/index.ts | 5 +++-- packages/hooks/src/useMount/index.zh-CN.md | 4 ++-- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/hooks/src/useMount/__tests__/index.test.ts b/packages/hooks/src/useMount/__tests__/index.test.ts index 24217f18cb..6d569da04f 100644 --- a/packages/hooks/src/useMount/__tests__/index.test.ts +++ b/packages/hooks/src/useMount/__tests__/index.test.ts @@ -3,16 +3,22 @@ import useMount from '../index'; describe('useMount', () => { it('test mount', async () => { + const destructor = jest.fn(); const fn = jest.fn(); + fn.mockReturnValue(destructor); const hook = renderHook(() => useMount(fn)); - expect(fn).toBeCalledTimes(1); + expect(fn).toHaveBeenCalledTimes(1); + expect(destructor).toHaveBeenCalledTimes(0); hook.rerender(); - expect(fn).toBeCalledTimes(1); + expect(fn).toHaveBeenCalledTimes(1); + expect(destructor).toHaveBeenCalledTimes(0); hook.unmount(); - expect(fn).toBeCalledTimes(1); + expect(fn).toHaveBeenCalledTimes(1); + expect(destructor).toHaveBeenCalledTimes(1); renderHook(() => useMount(fn)).unmount(); - expect(fn).toBeCalledTimes(2); + expect(fn).toHaveBeenCalledTimes(2); + expect(destructor).toHaveBeenCalledTimes(2); }); // it('should output error when fn is not a function', () => { diff --git a/packages/hooks/src/useMount/demo/demo1.tsx b/packages/hooks/src/useMount/demo/demo1.tsx index 0748bc5b61..1c6a0e480d 100644 --- a/packages/hooks/src/useMount/demo/demo1.tsx +++ b/packages/hooks/src/useMount/demo/demo1.tsx @@ -13,6 +13,10 @@ import React from 'react'; const MyComponent = () => { useMount(() => { message.info('mount'); + + return () => { + message.info('unmount'); + }; }); return
Hello World
; @@ -23,7 +27,7 @@ export default () => { return ( <> - {state && } diff --git a/packages/hooks/src/useMount/index.en-US.md b/packages/hooks/src/useMount/index.en-US.md index d4f5fe0956..76cbfff465 100644 --- a/packages/hooks/src/useMount/index.en-US.md +++ b/packages/hooks/src/useMount/index.en-US.md @@ -16,11 +16,11 @@ A hook that executes a function after the component is mounted. ## API ```typescript -useMount(fn: () => void); +useMount(fn: EffectCallback); ``` ### Params | Property | Description | Type | Default | | -------- | --------------------------- | ------------ | ------- | -| fn | The function to be executed | `() => void` | - | +| fn | The function to be executed | `EffectCallback` | - | diff --git a/packages/hooks/src/useMount/index.ts b/packages/hooks/src/useMount/index.ts index 1c437e2b59..3381042c58 100644 --- a/packages/hooks/src/useMount/index.ts +++ b/packages/hooks/src/useMount/index.ts @@ -1,8 +1,9 @@ import { useEffect } from 'react'; +import { type EffectCallback } from 'react'; import { isFunction } from '../utils'; import isDev from '../utils/isDev'; -const useMount = (fn: () => void) => { +const useMount = (fn: EffectCallback) => { if (isDev) { if (!isFunction(fn)) { console.error( @@ -12,7 +13,7 @@ const useMount = (fn: () => void) => { } useEffect(() => { - fn?.(); + return fn?.(); }, []); }; diff --git a/packages/hooks/src/useMount/index.zh-CN.md b/packages/hooks/src/useMount/index.zh-CN.md index 390edb4a06..55886c605b 100644 --- a/packages/hooks/src/useMount/index.zh-CN.md +++ b/packages/hooks/src/useMount/index.zh-CN.md @@ -16,11 +16,11 @@ nav: ## API ```typescript -useMount(fn: () => void); +useMount(fn: EffectCallback); ``` ### 参数 | 参数 | 说明 | 类型 | 默认值 | | ---- | ------------------ | ------------ | ------ | -| fn | 初始化时执行的函数 | `() => void` | - | +| fn | 初始化时执行的函数 | `EffectCallback` | - |