Skip to content

Commit

Permalink
fix: 改进内部 hook 对外暴露分类
Browse files Browse the repository at this point in the history
  • Loading branch information
Wxh16144 committed Mar 7, 2024
1 parent a9ca12f commit 8ff974e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './useBoolean';
export { default as useBoolean } from './useBoolean';

export { default as useLatestFunc } from './useLatestFunc';
export * from './useModalEnhanced';
export { default as useModalEnhanced } from './useModalEnhanced';
9 changes: 6 additions & 3 deletions src/util/use-latest-func.ts → src/hooks/useLatestFunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { useCallback, useEffect, useRef } from 'react';

type Maybe<T> = T | undefined | null;

// https://reactjs.org/docs/hooks-faq.html#what-can-i-do-if-my-effect-dependencies-change-too-often
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useLatestFunc<T extends Maybe<(...args: any[]) => any>>(fn: T): T {
/**
* @see https://reactjs.org/docs/hooks-faq.html#what-can-i-do-if-my-effect-dependencies-change-too-often
*/
function useLatestFunc<T extends Maybe<(...args: any[]) => any>>(fn: T): T {
const ref = useRef(fn);

useEffect(() => {
Expand All @@ -18,3 +19,5 @@ export function useLatestFunc<T extends Maybe<(...args: any[]) => any>>(fn: T):
// @ts-expect-error
return fn ? callbackFn : fn;
}

export default useLatestFunc;
3 changes: 2 additions & 1 deletion src/hooks/useModalEnhanced.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import { useEasyAntdModal } from '../context';
import type { AnyFunction, AnyObj } from '../types';
import { has, isDOMTypeElement, isElement, omit, useLatestFunc } from '../util';
import { has, isDOMTypeElement, isElement, omit } from '../util';
import useBoolean from './useBoolean';
import useLatestFunc from './useLatestFunc';

export type PropsWithModalEnhanced<
P extends AnyObj = AnyObj,
Expand Down
1 change: 0 additions & 1 deletion src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './has';
export * from './omit';
export * from './react-is';
export * from './use-latest-func';

0 comments on commit 8ff974e

Please sign in to comment.