File tree 5 files changed +111
-2
lines changed
5 files changed +111
-2
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import { useTimeout } from './useTimeout';
11
11
import { useToggle } from './useToggle' ;
12
12
import { useUnmount } from './useUnmount' ;
13
13
import { useUpdateEffect } from './useUpdateEffect' ;
14
+ import { useUpdateLayoutEffect } from './useUpdateLayoutEffect' ;
14
15
15
16
export {
16
17
useBoolean ,
@@ -24,7 +25,8 @@ export {
24
25
useTimeout ,
25
26
useToggle ,
26
27
useUnmount ,
27
- useUpdateEffect
28
+ useUpdateEffect ,
29
+ useUpdateLayoutEffect
28
30
} ;
29
31
30
32
export default {
@@ -39,5 +41,6 @@ export default {
39
41
useTimeout,
40
42
useToggle,
41
43
useUnmount,
42
- useUpdateEffect
44
+ useUpdateEffect,
45
+ useUpdateLayoutEffect
43
46
} ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * title: 基础用法
3
+ */
4
+
5
+ import React , { useLayoutEffect , useState } from 'react' ;
6
+ import { Button } from 'antd' ;
7
+ import { useUpdateLayoutEffect } from '@pansy/react-hooks' ;
8
+
9
+ export default ( ) => {
10
+ const [ count , setCount ] = useState ( 0 ) ;
11
+ const [ layoutEffectCount , setLayoutEffectCount ] = useState ( 0 ) ;
12
+ const [ updateLayoutEffectCount , setUpdateLayoutEffectCount ] = useState ( 0 ) ;
13
+
14
+ useLayoutEffect ( ( ) => {
15
+ setLayoutEffectCount ( ( c ) => c + 1 ) ;
16
+ } , [ count ] ) ;
17
+
18
+ useUpdateLayoutEffect ( ( ) => {
19
+ setUpdateLayoutEffectCount ( ( c ) => c + 1 ) ;
20
+ return ( ) => {
21
+ // do something
22
+ } ;
23
+ } , [ count ] ) ; // you can include deps array if necessary
24
+
25
+ return (
26
+ < div >
27
+ < p > layoutEffectCount: { layoutEffectCount } </ p >
28
+ < p > updateLayoutEffectCount: { updateLayoutEffectCount } </ p >
29
+ < p >
30
+ < Button onClick = { ( ) => setCount ( ( c ) => c + 1 ) } >
31
+ reRender
32
+ </ Button >
33
+ </ p >
34
+ </ div >
35
+ ) ;
36
+ } ;
Original file line number Diff line number Diff line change
1
+ /* eslint-disable max-nested-callbacks */
2
+ import { renderHook } from '@testing-library/react-hooks' ;
3
+ import { useUpdateLayoutEffect } from '../index' ;
4
+
5
+ describe ( 'useUpdateLayoutEffect' , ( ) => {
6
+ it ( 'should be defined' , ( ) => {
7
+ expect ( useUpdateLayoutEffect ) . toBeDefined ( ) ;
8
+ } ) ;
9
+
10
+ it ( 'test on mounted' , async ( ) => {
11
+ let mountedState = 1 ;
12
+ const hook = renderHook ( ( ) =>
13
+ useUpdateLayoutEffect ( ( ) => {
14
+ mountedState = 2 ;
15
+ } ) ,
16
+ ) ;
17
+ expect ( mountedState ) . toEqual ( 1 ) ;
18
+ hook . rerender ( ) ;
19
+ expect ( mountedState ) . toEqual ( 2 ) ;
20
+ } ) ;
21
+
22
+ it ( 'test on optional' , ( ) => {
23
+ let mountedState = 1 ;
24
+ const hook = renderHook ( ( ) =>
25
+ useUpdateLayoutEffect ( ( ) => {
26
+ mountedState = 3 ;
27
+ } , [ mountedState ] ) ,
28
+ ) ;
29
+ expect ( mountedState ) . toEqual ( 1 ) ;
30
+ hook . rerender ( ) ;
31
+ expect ( mountedState ) . toEqual ( 1 ) ;
32
+ mountedState = 2 ;
33
+ hook . rerender ( ) ;
34
+ expect ( mountedState ) . toEqual ( 3 ) ;
35
+ } ) ;
36
+ } ) ;
Original file line number Diff line number Diff line change
1
+ ---
2
+ title : useUpdateLayoutEffect
3
+ nav :
4
+ title : Hooks
5
+ path : /hooks
6
+ group :
7
+ title : Effect
8
+ path : /effect
9
+ ---
10
+
11
+ # useUpdateLayoutEffect
12
+
13
+ ` useUpdateLayoutEffect ` 用法等同于 ` useLayoutEffect ` ,但是会忽略首次执行,只在依赖更新时执行。
14
+
15
+ ## 代码演示
16
+
17
+ ### 基础用法
18
+
19
+ <code src =" ./__demo__/demo01.tsx " />
20
+
21
+ ## API
22
+
23
+ API 与 ` React.useLayoutEffect ` 完全一致。
24
+
25
+ ``` typescript
26
+ useUpdateLayoutEffect (
27
+ effect : React .EffectCallback ,
28
+ deps ?: React .DependencyList ,
29
+ )
30
+ ```
Original file line number Diff line number Diff line change
1
+ import { useLayoutEffect } from 'react' ;
2
+ import { createUpdateEffect } from '../utils/createUpdateEffect' ;
3
+
4
+ export const useUpdateLayoutEffect = createUpdateEffect ( useLayoutEffect ) ;
You can’t perform that action at this time.
0 commit comments