Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add jsdoc info #2406

Open
wants to merge 77 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
0a3feee
chore: add jsdoc
Wxh16144 Dec 17, 2023
e4693e4
chore: add jsdoc
Wxh16144 Dec 17, 2023
23ff7b9
chore: add jsdoc
Wxh16144 Dec 17, 2023
1539b36
chore: add jsdoc
Wxh16144 Dec 17, 2023
e33b88f
chore: add jsdoc
Wxh16144 Dec 17, 2023
07f2ba8
chore: add jsdoc
Wxh16144 Dec 17, 2023
ece4328
chore: add jsdoc
Wxh16144 Dec 17, 2023
fd82fb0
chore: add jsdoc
Wxh16144 Dec 17, 2023
893efc7
chore: add jsdoc
Wxh16144 Dec 17, 2023
f4e562c
chore: add jsdoc
Wxh16144 Dec 17, 2023
fcdcc37
chore: add jsdoc
Wxh16144 Dec 17, 2023
2ea3587
chore: add jsdoc
Wxh16144 Dec 17, 2023
0256a54
chore: add jsdoc
Wxh16144 Dec 17, 2023
f34d462
chore: add jsdoc
Wxh16144 Dec 17, 2023
8237ea3
chore: add jsdoc
Wxh16144 Dec 17, 2023
d55a94c
chore: add jsdoc
Wxh16144 Dec 17, 2023
5af4f5b
chore: add jsdoc
Wxh16144 Dec 17, 2023
359dc11
chore: add jsdoc
Wxh16144 Dec 17, 2023
3226979
chore: add jsdoc
Wxh16144 Dec 17, 2023
817575b
chore: add jsdoc
Wxh16144 Dec 17, 2023
320cca8
chore: add jsdoc
Wxh16144 Dec 17, 2023
80fe30c
chore: add jsdoc
Wxh16144 Dec 17, 2023
c63d247
chore: add jsdoc
Wxh16144 Dec 17, 2023
9b510ef
chore: add jsdoc
Wxh16144 Dec 17, 2023
71af327
chore: add jsdoc
Wxh16144 Dec 17, 2023
9ba017d
chore: add jsdoc
Wxh16144 Dec 17, 2023
88ede8b
chore: add jsdoc
Wxh16144 Dec 17, 2023
f9799cf
chore: add jsdoc
Wxh16144 Dec 17, 2023
0cbced8
chore: add jsdoc
Wxh16144 Dec 17, 2023
18253fe
chore: add jsdoc
Wxh16144 Dec 17, 2023
72590e3
chore: add jsdoc
Wxh16144 Dec 17, 2023
63d5b1c
chore: add jsdoc
Wxh16144 Dec 17, 2023
97e77d0
chore: add jsdoc
Wxh16144 Dec 17, 2023
979c25e
chore: add jsdoc
Wxh16144 Dec 17, 2023
7703b71
chore: add jsdoc
Wxh16144 Dec 17, 2023
b7561c2
chore: add jsdoc
Wxh16144 Dec 17, 2023
85acff6
chore: add jsdoc
Wxh16144 Dec 17, 2023
b804ae7
chore: add jsdoc
Wxh16144 Dec 17, 2023
28d5ba4
chore: add jsdoc
Wxh16144 Dec 17, 2023
fdf57c3
chore: add jsdoc
Wxh16144 Dec 17, 2023
316a0f5
chore: add jsdoc
Wxh16144 Dec 17, 2023
5962d32
chore: add jsdoc
Wxh16144 Dec 17, 2023
72e96e4
chore: add jsdoc
Wxh16144 Dec 17, 2023
7b31348
chore: add jsdoc
Wxh16144 Dec 17, 2023
ed94a34
chore: add jsdco
Wxh16144 Dec 17, 2023
258d3b4
chore: add jsdoc
Wxh16144 Dec 17, 2023
4d72b33
chore: add jsdoc
Wxh16144 Dec 17, 2023
97c75a8
chore: add jsdoc
Wxh16144 Dec 17, 2023
f4db0cb
chore: add jsdoc
Wxh16144 Dec 17, 2023
4ec9598
chore: add jsdoc
Wxh16144 Dec 17, 2023
0274279
chore: update jsdoc
Wxh16144 Dec 17, 2023
5c05b19
chore: add jsdoc
Wxh16144 Dec 17, 2023
48652e7
chore: add jsdoc
Wxh16144 Dec 17, 2023
447e971
chore: add jsdoc
Wxh16144 Dec 17, 2023
77d5466
chore: add jsdoc
Wxh16144 Dec 17, 2023
fb8c81f
chore: add jsdoc
Wxh16144 Dec 17, 2023
8842335
chore: add jsdoc
Wxh16144 Dec 17, 2023
e64a76f
chore: add jsdoc
Wxh16144 Dec 17, 2023
dc09e90
chore: add jsdoc
Wxh16144 Dec 17, 2023
04e6e8c
chore: add jsdoc
Wxh16144 Dec 17, 2023
938c8f2
chore: add jsdoc
Wxh16144 Dec 17, 2023
7991086
chore: add jsdoc
Wxh16144 Dec 17, 2023
e0450b0
chore: add jsdoc
Wxh16144 Dec 17, 2023
a8518b0
chore: add jsdoc
Wxh16144 Dec 17, 2023
aa6f772
chore: add jsdoc
Wxh16144 Dec 17, 2023
4fc1895
chore: add jsdoc
Wxh16144 Dec 17, 2023
852106f
chore: add jsdoc
Wxh16144 Dec 17, 2023
63e3454
chore: add jsdoc
Wxh16144 Dec 17, 2023
841e667
chore: add jsdoc
Wxh16144 Dec 17, 2023
22867b2
chore: add jsdoc
Wxh16144 Dec 17, 2023
528d395
chore: add jsdoc
Wxh16144 Dec 17, 2023
292c9f0
chore: add jsdoc
Wxh16144 Dec 17, 2023
9f822c3
chore: add jsdoc
Wxh16144 Dec 17, 2023
acc013e
chore: add jsdoc
Wxh16144 Dec 17, 2023
bc674c9
chore: add jsdoc
Wxh16144 Dec 17, 2023
b6ae8f9
chore: add jsdoc
Wxh16144 Dec 17, 2023
e2a9186
chore: revert diff
Wxh16144 Dec 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/hooks/src/useAntdTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import type {
AntdTableResult,
} from './types';

/**
* `useAntdTable` is implemented based on [useRequest](https://ahooks.js.org/hooks/use-request) and encapsulates the commonly used [Ant Design Form](https://ant.design/components/form/) and [Ant Design Table](https://ant.design/components/table/) data binding logic, and supports both antd v3 and v4.
* @see https://ahooks.js.org/hooks/use-antd-table
*/
const useAntdTable = <TData extends Data, TParams extends Params>(
service: Service<TData, TParams>,
options: AntdTableOptions<TData, TParams> = {},
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useAsyncEffect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function isAsyncGenerator(
return isFunction(val[Symbol.asyncIterator]);
}

/**
* useEffect support async function.
* @see https://ahooks.js.org/hooks/use-async-effect
*/
function useAsyncEffect(
effect: () => AsyncGenerator<void, void, void> | Promise<void>,
deps?: DependencyList,
Expand Down
8 changes: 8 additions & 0 deletions packages/hooks/src/useBoolean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export interface Actions {
toggle: () => void;
}

/**
* Elegantly manages boolean state.
* @see https://ahooks.js.org/hooks/use-boolean
* @example
* ```jsx
* const [state, { toggle, setTrue, setFalse }] = useBoolean(true);
* ```
*/
export default function useBoolean(defaultValue = false): [boolean, Actions] {
const [state, { toggle, set }] = useToggle(!!defaultValue);

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useClickAway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import useEffectWithTarget from '../utils/useEffectWithTarget';

type DocumentEventKey = keyof DocumentEventMap;

/**
* Listen for click events outside the target element.
* @see https://ahooks.js.org/hooks/use-click-away
*/
export default function useClickAway<T extends Event = Event>(
onClickAway: (event: T) => void,
target: BasicTarget | BasicTarget[],
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useControllableValue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export interface StandardProps<T> {
onChange: (val: T) => void;
}

/**
* Controlled and uncontrolled
* @see https://ahooks.js.org/hooks/use-controllable-value)
*/
function useControllableValue<T = any>(
props: StandardProps<T>,
): [T, (v: SetStateAction<T>) => void];
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useCookieState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export interface Options extends Cookies.CookieAttributes {
defaultValue?: State | (() => State);
}

/**
* A Hook that store state into Cookie.
* @see https://ahooks.js.org/hooks/use-cookie-state
*/
function useCookieState(cookieKey: string, options: Options = {}) {
const [state, setState] = useState<State>(() => {
const cookieValue = Cookies.get(cookieKey);
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useCountDown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const parseMs = (milliseconds: number): FormattedRes => {
};
};

/**
* A hook for manage countdown.
* @see https://ahooks.js.org/hooks/use-count-down
*/
const useCountdown = (options: Options = {}) => {
const { leftTime, targetDate, interval = 1000, onEnd } = options || {};

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useCounter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function getTargetValue(val: number, options: Options = {}) {
return target;
}

/**
* A hook that manage counter.
* @see https://ahooks.js.org/hooks/use-counter
*/
function useCounter(initialValue: number = 0, options: Options = {}) {
const { min, max } = options;

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useCreation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type { DependencyList } from 'react';
import { useRef } from 'react';
import depsAreSame from '../utils/depsAreSame';

/**
* `useCreation` is the replacement for `useMemo` or `useRef`.
* @see https://ahooks.js.org/hooks/use-creation
*/
export default function useCreation<T>(factory: () => T, deps: DependencyList) {
const { current } = useRef({
deps,
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useDebounce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { useEffect, useState } from 'react';
import useDebounceFn from '../useDebounceFn';
import type { DebounceOptions } from './debounceOptions';

/**
* A hook that deal with the debounced value.
* @see https://ahooks.js.org/hooks/use-debounce
*/
function useDebounce<T>(value: T, options?: DebounceOptions) {
const [debounced, setDebounced] = useState(value);

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useDebounceEffect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type { DebounceOptions } from '../useDebounce/debounceOptions';
import useDebounceFn from '../useDebounceFn';
import useUpdateEffect from '../useUpdateEffect';

/**
* Debounce your `useEffect`.
* @see https://ahooks.js.org/hooks/use-debounce-effect
*/
function useDebounceEffect(
effect: EffectCallback,
deps?: DependencyList,
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useDebounceFn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import isDev from '../utils/isDev';

type noop = (...args: any[]) => any;

/**
* A hook that deal with the debounced function.
* @see https://ahooks.js.org/hooks/use-debounce-fn
*/
function useDebounceFn<T extends noop>(fn: T, options?: DebounceOptions) {
if (isDev) {
if (!isFunction(fn)) {
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useDeepCompareEffect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { useEffect } from 'react';
import { createDeepCompareEffect } from '../createDeepCompareEffect';

/**
* Usage is the same as `useEffect`, but deps are compared with [lodash.isEqual](https://lodash.com/docs/4.17.15#isEqual).
* @see https://ahooks.js.org/hooks/use-deep-compare-effect
*/
export default createDeepCompareEffect(useEffect);
4 changes: 4 additions & 0 deletions packages/hooks/src/useDeepCompareLayoutEffect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { useLayoutEffect } from 'react';
import { createDeepCompareEffect } from '../createDeepCompareEffect';

/**
* Usage is the same as `useLayoutEffect`, but deps are compared with [lodash.isEqual](https://lodash.com/docs/4.17.15#isEqual).
* @see https://ahooks.js.org/hooks/use-deep-compare-layout-effect
*/
export default createDeepCompareEffect(useLayoutEffect);
4 changes: 4 additions & 0 deletions packages/hooks/src/useDocumentVisibility/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const getVisibility = () => {
return document.visibilityState;
};

/**
* A Hook can tell if the page is visible, refer to [visibilityState API](https://developer.mozilla.org/docs/Web/API/Document/visibilityState)
* @see https://ahooks.js.org/hooks/use-document-visibility
*/
function useDocumentVisibility(): VisibilityState {
const [documentVisibility, setDocumentVisibility] = useState(() => getVisibility());

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useDrag/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface Options {
};
}

/**
* help you manage data transfer between drag and drop
* @see https://ahooks.js.org/hooks/use-drop
*/
const useDrag = <T>(data: T, target: BasicTarget, options: Options = {}) => {
const optionsRef = useLatest(options);
const dataRef = useLatest(data);
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useDrop/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface Options {
onPaste?: (event?: React.ClipboardEvent) => void;
}

/**
* help you manage data transfer between drag and drop
* @see https://ahooks.js.org/hooks/use-drop
*/
const useDrop = (target: BasicTarget, options: Options = {}) => {
const optionsRef = useLatest(options);

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useDynamicList/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { useCallback, useRef, useState } from 'react';

/**
* A hook that helps you manage dynamic list and generate unique key for each item.
* @see https://ahooks.js.org/hooks/use-dynamic-list
*/
const useDynamicList = <T>(initialList: T[] = []) => {
const counterRef = useRef(-1);

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useEventEmitter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class EventEmitter<T> {
};
}

/**
* Simplifying multiple component passing of parameters
* @see https://ahooks.js.org/hooks/use-event-emitter
*/
export default function useEventEmitter<T = void>() {
const ref = useRef<EventEmitter<T>>();
if (!ref.current) {
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useEventListener/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type Options<T extends Target = Target> = {
passive?: boolean;
};

/**
* Use addEventListener elegant by Hook.
* @see https://ahooks.js.org/hooks/use-event-listener
*/
function useEventListener<K extends keyof HTMLElementEventMap>(
eventName: K,
handler: (ev: HTMLElementEventMap[K]) => void,
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useEventTarget/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface Options<T, U> {
transformer?: (value: U) => T;
}

/**
* A hook that encapsulates `onChange` and `value` logic for form controls that obtains value through `event.target.value`.
* @see https://ahooks.js.org/hooks/use-event-target
*/
function useEventTarget<T, U = T>(options?: Options<T, U>) {
const { initialValue, transformer } = options || {};
const [value, setValue] = useState(initialValue);
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useExternal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ const loadCss = (path: string, props = {}): loadResult => {
};
};

/**
* Dynamically load JS or CSS, useExternal can ensure that the resource are globally unique.
* @see https://ahooks.js.org/hooks/use-external
*/
const useExternal = (path?: string, options?: Options) => {
const [status, setStatus] = useState<Status>(path ? 'loading' : 'unset');

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useFavicon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const ImgTypeMap = {

type ImgTypes = keyof typeof ImgTypeMap;

/**
* A hook that set the favicon of the page.
* @see https://ahooks.js.org/hooks/use-favicon
*/
const useFavicon = (href: string) => {
useEffect(() => {
if (!href) return;
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useFocusWithin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface Options {
onChange?: (isFocusWithin: boolean) => void;
}

/**
* Monitor whether the current focus is within a certain area, Same as css attribute [:focus-within](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-within).
* @see https://ahooks.js.org/hooks/use-focus-within
*/
export default function useFocusWithin(target: BasicTarget, options?: Options) {
const [isFocusWithin, setIsFocusWithin] = useState(false);
const { onFocus, onBlur, onChange } = options || {};
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useFullscreen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export interface Options {
pageFullscreen?: boolean | PageFullscreenOptions;
}

/**
* manages DOM full screen.
* @see https://ahooks.js.org/hooks/use-fullscreen
*/
const useFullscreen = (target: BasicTarget, options?: Options) => {
const { onExit, onEnter, pageFullscreen = false } = options || {};
const { className = 'ahooks-page-fullscreen', zIndex = 999999 } =
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useFusionTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import type { Data, Params, Service } from '../useAntdTable/types';
import { fieldAdapter, resultAdapter } from './fusionAdapter';
import type { FusionTableOptions, FusionTableResult } from './types';

/**
* `useFusionTable` encapsulates the commonly used [Fusion Form](https://fusion.design/pc/component/basic/form) and [Fusion Table](https://fusion.design/pc/component/basic/table) data binding logic.
* @see https://ahooks.js.org/hooks/use-fusion-table
*/
const useFusionTable = <TData extends Data, TParams extends Params>(
service: Service<TData, TParams>,
options: FusionTableOptions<TData, TParams> = {},
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useGetState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { useState, useRef, useCallback } from 'react';

type GetStateAction<S> = () => S;

/**
* Add a getter method to the return value of `React.useState` to get the latest value
* @see https://ahooks.js.org/hooks/use-get-state
*/
function useGetState<S>(
initialState: S | (() => S),
): [S, Dispatch<SetStateAction<S>>, GetStateAction<S>];
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useHistoryTravel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const split = <T>(step: number, targetArr: T[]) => {
};
};

/**
* A hook to manage state change history. It provides encapsulation methods to travel through the history.
* @see https://ahooks.js.org/hooks/use-history-travel
*/
export default function useHistoryTravel<T>(initialValue?: T, maxLength: number = 0) {
const [history, setHistory] = useState<IData<T | undefined>>({
present: initialValue,
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useHover/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface Options {
onChange?: (isHovering: boolean) => void;
}

/**
* A hook that tracks whether the element is being hovered.
* @see https://ahooks.js.org/hooks/use-hover
*/
export default (target: BasicTarget, options?: Options): boolean => {
const { onEnter, onLeave, onChange } = options || {};

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useInViewport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface Options {
callback?: CallbackType;
}

/**
* Observe whether the element is in the visible area, and the visible area ratio of the element. refer to [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
* @see https://ahooks.js.org/hooks/use-in-viewport
*/
function useInViewport(target: BasicTarget | BasicTarget[], options?: Options) {
const { callback, ...option } = options || {};

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useInfiniteScroll/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { getTargetElement } from '../utils/domTarget';
import { getClientHeight, getScrollHeight, getScrollTop } from '../utils/rect';
import type { Data, InfiniteScrollOptions, Service } from './types';

/**
* `useInfiniteScroll` encapsulates the common infinite scroll logic.
* @see https://ahooks.js.org/hooks/use-infinite-scroll
*/
const useInfiniteScroll = <TData extends Data>(
service: Service<TData>,
options: InfiniteScrollOptions<TData> = {},
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useInterval/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { useCallback, useEffect, useRef } from 'react';
import useMemoizedFn from '../useMemoizedFn';
import { isNumber } from '../utils';

/**
* A hook that handles the `setInterval` timer function.
* @see https://ahooks.js.org/hooks/use-interval
*/
const useInterval = (fn: () => void, delay?: number, options: { immediate?: boolean } = {}) => {
const timerCallback = useMemoizedFn(fn);
const timerRef = useRef<NodeJS.Timer | null>(null);
Expand Down
20 changes: 20 additions & 0 deletions packages/hooks/src/useIsomorphicLayoutEffect/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import { useEffect, useLayoutEffect } from 'react';
import isBrowser from '../utils/isBrowser';

/**
* Return useLayoutEffect for browser environment and useEffect for other environments.
* @description
* In SSR mode, the following warning will appear when useLayoutEffect is used
*
* > ⚠️ Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client. See https://fb.me/react-uselayouteffect-ssr for common fixes.
*
* To avoid this warning, `useIsomorphicLayoutEffect` can be used instead of useLayoutEffect.
*
* The source code:
*
* ```javascript
* const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;
* ```
*
* Return useLayoutEffect for browser environment and useEffect for other environments.
*
* For more information, please refer to [useLayoutEffect and SSR](https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a)
* @see https://ahooks.js.org/hooks/use-isomorphic-layout-effect
*/
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;

export default useIsomorphicLayoutEffect;
4 changes: 4 additions & 0 deletions packages/hooks/src/useKeyPress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ function genKeyFormatter(keyFilter: KeyFilter, exactMatch: boolean): KeyPredicat

const defaultEvents: KeyEvent[] = ['keydown'];

/**
* Listen for the keyboard press, support key combinations, and support alias.
* @see https://ahooks.js.org/hooks/use-key-press
*/
function useKeyPress(
keyFilter: KeyFilter,
eventHandler: (event: KeyboardEvent, key: KeyType) => void,
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useLatest/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { useRef } from 'react';

/**
* A Hook that returns the latest value, effectively avoiding the closure problem.
* @see https://ahooks.js.org/hooks/use-latest
*/
function useLatest<T>(value: T) {
const ref = useRef(value);
ref.current = value;
Expand Down