Skip to content

Commit

Permalink
Merge branch 'master' of github.com:alibaba/hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
brickspert committed Nov 17, 2021
2 parents c84ae31 + 2c00eff commit 09014d4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/hooks/src/useMount/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { useEffect } from 'react';
const useMount = (fn: () => void) => {
if (process.env.NODE_ENV === 'development') {
if (typeof fn !== 'function') {
console.error(`useUnmount expected parameter is a function, got ${typeof fn}`);
console.error(`useMount: parameter \`fn\` expected to be a function, but got "${typeof fn}".`);
}
}

useEffect(() => {
fn();
fn?.();
}, []);
};

Expand Down
14 changes: 9 additions & 5 deletions packages/hooks/src/utils/domTarget.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type { MutableRefObject } from 'react';
import isBrowser from './isBrowser';

type Target<T> = T | undefined | null;
type TargetValue<T> = T | undefined | null;

export type BasicTarget<T = Element> = (() => Target<T>) | Target<T> | MutableRefObject<Target<T>>;
type TargetType = HTMLElement | Element | Window | Document;

export function getTargetElement<T>(target: BasicTarget<T>, defaultElement?: T) {
export type BasicTarget<T extends TargetType = Element> =
| (() => TargetValue<T>)
| TargetValue<T>
| MutableRefObject<TargetValue<T>>;

export function getTargetElement<T extends TargetType>(target: BasicTarget<T>, defaultElement?: T) {
if (!isBrowser) {
return undefined;
}
Expand All @@ -14,10 +19,9 @@ export function getTargetElement<T>(target: BasicTarget<T>, defaultElement?: T)
return defaultElement;
}

let targetElement: Target<T>;
let targetElement: TargetValue<T>;

if (typeof target === 'function') {
// @ts-ignore
targetElement = target();
} else if ('current' in target) {
targetElement = target.current;
Expand Down

0 comments on commit 09014d4

Please sign in to comment.