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

feat: usemount support destructor #2713

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions packages/hooks/src/useMount/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/hooks/src/useMount/demo/demo1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import React from 'react';
const MyComponent = () => {
useMount(() => {
message.info('mount');

return () => {
message.info('unmount');
};
});

return <div>Hello World</div>;
Expand All @@ -23,7 +27,7 @@ export default () => {

return (
<>
<button type="button" onClick={toggle}>
<button type='button' onClick={toggle}>
{state ? 'unmount' : 'mount'}
</button>
{state && <MyComponent />}
Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/src/useMount/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | - |
5 changes: 3 additions & 2 deletions packages/hooks/src/useMount/index.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -12,7 +13,7 @@ const useMount = (fn: () => void) => {
}

useEffect(() => {
fn?.();
return fn?.();
}, []);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/src/useMount/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ nav:
## API

```typescript
useMount(fn: () => void);
useMount(fn: EffectCallback);
```

### 参数

| 参数 | 说明 | 类型 | 默认值 |
| ---- | ------------------ | ------------ | ------ |
| fn | 初始化时执行的函数 | `() => void` | - |
| fn | 初始化时执行的函数 | `EffectCallback` | - |