File tree Expand file tree Collapse file tree 5 files changed +22
-11
lines changed
packages/hooks/src/useMount Expand file tree Collapse file tree 5 files changed +22
-11
lines changed Original file line number Diff line number Diff line change @@ -3,16 +3,22 @@ import useMount from '../index';
3
3
4
4
describe ( 'useMount' , ( ) => {
5
5
it ( 'test mount' , async ( ) => {
6
+ const destructor = jest . fn ( ) ;
6
7
const fn = jest . fn ( ) ;
8
+ fn . mockReturnValue ( destructor ) ;
7
9
const hook = renderHook ( ( ) => useMount ( fn ) ) ;
8
- expect ( fn ) . toBeCalledTimes ( 1 ) ;
10
+ expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
11
+ expect ( destructor ) . toHaveBeenCalledTimes ( 0 ) ;
9
12
hook . rerender ( ) ;
10
- expect ( fn ) . toBeCalledTimes ( 1 ) ;
13
+ expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
14
+ expect ( destructor ) . toHaveBeenCalledTimes ( 0 ) ;
11
15
hook . unmount ( ) ;
12
- expect ( fn ) . toBeCalledTimes ( 1 ) ;
16
+ expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
17
+ expect ( destructor ) . toHaveBeenCalledTimes ( 1 ) ;
13
18
14
19
renderHook ( ( ) => useMount ( fn ) ) . unmount ( ) ;
15
- expect ( fn ) . toBeCalledTimes ( 2 ) ;
20
+ expect ( fn ) . toHaveBeenCalledTimes ( 2 ) ;
21
+ expect ( destructor ) . toHaveBeenCalledTimes ( 2 ) ;
16
22
} ) ;
17
23
18
24
// it('should output error when fn is not a function', () => {
Original file line number Diff line number Diff line change @@ -13,6 +13,10 @@ import React from 'react';
13
13
const MyComponent = ( ) => {
14
14
useMount ( ( ) => {
15
15
message . info ( 'mount' ) ;
16
+
17
+ return ( ) => {
18
+ message . info ( 'unmount' ) ;
19
+ } ;
16
20
} ) ;
17
21
18
22
return < div > Hello World</ div > ;
@@ -23,7 +27,7 @@ export default () => {
23
27
24
28
return (
25
29
< >
26
- < button type = " button" onClick = { toggle } >
30
+ < button type = ' button' onClick = { toggle } >
27
31
{ state ? 'unmount' : 'mount' }
28
32
</ button >
29
33
{ state && < MyComponent /> }
Original file line number Diff line number Diff line change @@ -16,11 +16,11 @@ A hook that executes a function after the component is mounted.
16
16
## API
17
17
18
18
``` typescript
19
- useMount (fn : () => void );
19
+ useMount (fn : EffectCallback );
20
20
```
21
21
22
22
### Params
23
23
24
24
| Property | Description | Type | Default |
25
25
| -------- | --------------------------- | ------------ | ------- |
26
- | fn | The function to be executed | ` () => void ` | - |
26
+ | fn | The function to be executed | ` EffectCallback ` | - |
Original file line number Diff line number Diff line change 1
1
import { useEffect } from 'react' ;
2
+ import { type EffectCallback } from 'react' ;
2
3
import { isFunction } from '../utils' ;
3
4
import isDev from '../utils/isDev' ;
4
5
5
- const useMount = ( fn : ( ) => void ) => {
6
+ const useMount = ( fn : EffectCallback ) => {
6
7
if ( isDev ) {
7
8
if ( ! isFunction ( fn ) ) {
8
9
console . error (
@@ -12,7 +13,7 @@ const useMount = (fn: () => void) => {
12
13
}
13
14
14
15
useEffect ( ( ) => {
15
- fn ?.( ) ;
16
+ return fn ?.( ) ;
16
17
} , [ ] ) ;
17
18
} ;
18
19
Original file line number Diff line number Diff line change 16
16
## API
17
17
18
18
``` typescript
19
- useMount (fn : () => void );
19
+ useMount (fn : EffectCallback );
20
20
```
21
21
22
22
### 参数
23
23
24
24
| 参数 | 说明 | 类型 | 默认值 |
25
25
| ---- | ------------------ | ------------ | ------ |
26
- | fn | 初始化时执行的函数 | ` () => void ` | - |
26
+ | fn | 初始化时执行的函数 | ` EffectCallback ` | - |
You can’t perform that action at this time.
0 commit comments