Skip to content

Commit e3e2448

Browse files
authored
Merge branch 'master' into react-19-up
2 parents dcabdc2 + 7558cb8 commit e3e2448

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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",
@@ -56,10 +56,10 @@
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",

src/hooks/useEvent.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
/* eslint-disable react-hooks/exhaustive-deps */
33
import * 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

tests/hooks.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import useMobile from '../src/hooks/useMobile';
99
import useState from '../src/hooks/useState';
1010
import useSyncState from '../src/hooks/useSyncState';
1111
import useControlledState from '../src/hooks/useControlledState';
12+
import useEvent from '../src/hooks/useEvent';
1213

1314
global.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
});

0 commit comments

Comments
 (0)