File tree Expand file tree Collapse file tree 3 files changed +33
-8
lines changed
Expand file tree Collapse file tree 3 files changed +33
-8
lines changed Original file line number Diff line number Diff line change 11{
22 "name" : " @rc-component/util" ,
3- "version" : " 1.3.0 " ,
3+ "version" : " 1.3.1 " ,
44 "description" : " Common Utils For React Component" ,
55 "keywords" : [
66 " react" ,
5656 "cross-env" : " ^7.0.2" ,
5757 "dumi" : " ^2.1.3" ,
5858 "eslint" : " ^8.54.0" ,
59- "eslint-plugin-jest" : " ^28.2.0 " ,
59+ "eslint-plugin-jest" : " ^29.0.1 " ,
6060 "eslint-plugin-unicorn" : " ^56.0.1" ,
6161 "father" : " ^4.1.3" ,
62- "glob" : " ^9.2.1 " ,
62+ "glob" : " ^11.0.3 " ,
6363 "husky" : " ^9.1.6" ,
6464 "lint-staged" : " ^15.1.0" ,
6565 "prettier" : " ^3.3.2" ,
Original file line number Diff line number Diff line change 22/* eslint-disable react-hooks/exhaustive-deps */
33import * as React from 'react' ;
44
5- function useEvent < T extends Function > ( callback : T ) : T {
6- const fnRef = React . useRef < T > ( null ) ;
7-
5+ function useEvent < T extends ( ( ...args : any [ ] ) => any ) | undefined > (
6+ callback : T ,
7+ ) : undefined extends T
8+ ? (
9+ ...args : Parameters < NonNullable < T > >
10+ ) => ReturnType < NonNullable < T > > | undefined
11+ : T {
12+ const fnRef = React . useRef < T | undefined > ( callback ) ;
813 fnRef . current = callback ;
914
10- const memoFn = React . useCallback < T > (
11- ( ( ...args : any ) => fnRef . current ?.( ...args ) ) as any ,
15+ const memoFn = React . useCallback (
16+ ( ...args : any [ ] ) => fnRef . current ?.( ...args ) ,
1217 [ ] ,
1318 ) ;
1419
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import useMobile from '../src/hooks/useMobile';
99import useState from '../src/hooks/useState' ;
1010import useSyncState from '../src/hooks/useSyncState' ;
1111import useControlledState from '../src/hooks/useControlledState' ;
12+ import useEvent from '../src/hooks/useEvent' ;
1213
1314global . disableUseId = false ;
1415
@@ -706,4 +707,23 @@ describe('hooks', () => {
706707 expect ( container . textContent ) . toEqual ( '2' ) ;
707708 } ) ;
708709 } ) ;
710+
711+ describe ( 'useEvent' , ( ) => {
712+ it ( 'extract type' , ( ) => {
713+ const Demo = ( props : {
714+ canUndefined ?: ( a : number ) => boolean ;
715+ notUndefined : ( a : number ) => boolean ;
716+ } ) => {
717+ const ua = useEvent ( props . canUndefined ) ;
718+ const ub = useEvent ( props . notUndefined ) ;
719+
720+ ua ( 1 ) ;
721+ ub ( 2 ) ;
722+
723+ return null ;
724+ } ;
725+
726+ expect ( Demo ) . toBeTruthy ( ) ;
727+ } ) ;
728+ } ) ;
709729} ) ;
You can’t perform that action at this time.
0 commit comments