@d-matrix/utils 1.27.1 → 1.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/dist/algorithm/binary.d.ts +8 -8
  2. package/dist/algorithm/binary.js +38 -38
  3. package/dist/algorithm/index.d.ts +2 -2
  4. package/dist/algorithm/index.js +2 -2
  5. package/dist/algorithm/tree.d.ts +34 -34
  6. package/dist/algorithm/tree.js +124 -124
  7. package/dist/array.d.ts +46 -5
  8. package/dist/array.js +129 -37
  9. package/dist/clipboard.d.ts +12 -12
  10. package/dist/clipboard.js +107 -107
  11. package/dist/color.d.ts +7 -7
  12. package/dist/color.js +41 -41
  13. package/dist/date.d.ts +34 -34
  14. package/dist/date.js +62 -62
  15. package/dist/decimal.d.ts +17 -17
  16. package/dist/decimal.js +23 -23
  17. package/dist/dom.d.ts +11 -11
  18. package/dist/dom.js +27 -27
  19. package/dist/echarts.d.ts +36 -36
  20. package/dist/echarts.js +112 -112
  21. package/dist/file.d.ts +49 -49
  22. package/dist/file.js +154 -154
  23. package/dist/i18n.d.ts +10 -10
  24. package/dist/i18n.js +12 -12
  25. package/dist/index.d.ts +16 -16
  26. package/dist/index.js +16 -16
  27. package/dist/number.d.ts +2 -2
  28. package/dist/number.js +4 -4
  29. package/dist/object.d.ts +13 -13
  30. package/dist/object.js +28 -28
  31. package/dist/operator.d.ts +6 -6
  32. package/dist/operator.js +6 -6
  33. package/dist/react/enhancedComponent.d.ts +12 -12
  34. package/dist/react/enhancedComponent.js +16 -16
  35. package/dist/react/index.d.ts +15 -12
  36. package/dist/react/index.js +15 -12
  37. package/dist/react/renderToString.d.ts +3 -3
  38. package/dist/react/renderToString.js +30 -30
  39. package/dist/react/types.d.ts +9 -9
  40. package/dist/react/types.js +1 -1
  41. package/dist/react/useCopyToClipboard.d.ts +21 -21
  42. package/dist/react/useCopyToClipboard.js +44 -44
  43. package/dist/react/useDeepCompareRef.d.ts +2 -2
  44. package/dist/react/useDeepCompareRef.js +11 -11
  45. package/dist/react/useDisableContextMenu.d.ts +7 -7
  46. package/dist/react/useDisableContextMenu.js +24 -24
  47. package/dist/react/useEventCallback.d.ts +7 -0
  48. package/dist/react/useEventCallback.js +19 -0
  49. package/dist/react/useForwardRef.d.ts +9 -9
  50. package/dist/react/useForwardRef.js +22 -22
  51. package/dist/react/useId.d.ts +1 -0
  52. package/dist/react/useId.js +12 -0
  53. package/dist/react/useIsFirstRender.d.ts +6 -6
  54. package/dist/react/useIsFirstRender.js +14 -14
  55. package/dist/react/useIsMounted.d.ts +2 -2
  56. package/dist/react/useIsMounted.js +13 -13
  57. package/dist/react/useIsomorphicLayoutEffect.d.ts +2 -2
  58. package/dist/react/useIsomorphicLayoutEffect.js +2 -2
  59. package/dist/react/useMediaQuery.d.ts +14 -14
  60. package/dist/react/useMediaQuery.js +42 -42
  61. package/dist/react/useSafeTimeout.d.ts +12 -0
  62. package/dist/react/useSafeTimeout.js +28 -0
  63. package/dist/react/useStateCallback.d.ts +2 -2
  64. package/dist/react/useStateCallback.js +17 -17
  65. package/dist/support.d.ts +12 -12
  66. package/dist/support.js +12 -12
  67. package/dist/timer.d.ts +5 -5
  68. package/dist/timer.js +5 -5
  69. package/dist/types.d.ts +128 -21
  70. package/dist/types.js +1 -1
  71. package/package.json +1 -1
  72. package/readme.md +5 -1
@@ -1,12 +1,15 @@
1
- export * from './useDisableContextMenu';
2
- export * from './useStateCallback';
3
- export * from './renderToString';
4
- export * from './useIsMounted';
5
- export * from './useCopyToClipboard';
6
- export * from './enhancedComponent';
7
- export * from './useDeepCompareRef';
8
- export * from './types';
9
- export * from './useForwardRef';
10
- export * from './useIsomorphicLayoutEffect';
11
- export * from './useMediaQuery';
12
- export * from './useIsFirstRender';
1
+ export * from './useDisableContextMenu';
2
+ export * from './useStateCallback';
3
+ export * from './renderToString';
4
+ export * from './useIsMounted';
5
+ export * from './useCopyToClipboard';
6
+ export * from './enhancedComponent';
7
+ export * from './useDeepCompareRef';
8
+ export * from './types';
9
+ export * from './useForwardRef';
10
+ export * from './useIsomorphicLayoutEffect';
11
+ export * from './useMediaQuery';
12
+ export * from './useIsFirstRender';
13
+ export * from './useStateCallback';
14
+ export * from './useSafeTimeout';
15
+ export * from './useId';
@@ -1,3 +1,3 @@
1
- import { ReactElement } from 'react';
2
- export declare const render: <P>(element: ReactElement<P, string | import("react").JSXElementConstructor<any>>) => Promise<string>;
3
- export declare const cleanup: () => void;
1
+ import { ReactElement } from 'react';
2
+ export declare const render: <P>(element: ReactElement<P, string | import("react").JSXElementConstructor<any>>) => Promise<string>;
3
+ export declare const cleanup: () => void;
@@ -1,30 +1,30 @@
1
- import ReactDOM from 'react-dom';
2
- let container;
3
- export const render = (element) => {
4
- return new Promise((resolve, reject) => {
5
- if (document === undefined) {
6
- return reject('只支持浏览器环境');
7
- }
8
- container = document.createElement('div');
9
- container.id = 'template-container';
10
- container.style.cssText = 'position: absolute; left: -9999px;';
11
- document.body.appendChild(container);
12
- ReactDOM.render(element, container, () => {
13
- if (container) {
14
- resolve(container.innerHTML);
15
- }
16
- else {
17
- reject('组件容器不存在');
18
- }
19
- });
20
- });
21
- };
22
- export const cleanup = () => {
23
- if (container === null)
24
- return;
25
- const isUnmounted = ReactDOM.unmountComponentAtNode(container);
26
- if (isUnmounted) {
27
- document.body.removeChild(container);
28
- container = null;
29
- }
30
- };
1
+ import ReactDOM from 'react-dom';
2
+ let container;
3
+ export const render = (element) => {
4
+ return new Promise((resolve, reject) => {
5
+ if (document === undefined) {
6
+ return reject('只支持浏览器环境');
7
+ }
8
+ container = document.createElement('div');
9
+ container.id = 'template-container';
10
+ container.style.cssText = 'position: absolute; left: -9999px;';
11
+ document.body.appendChild(container);
12
+ ReactDOM.render(element, container, () => {
13
+ if (container) {
14
+ resolve(container.innerHTML);
15
+ }
16
+ else {
17
+ reject('组件容器不存在');
18
+ }
19
+ });
20
+ });
21
+ };
22
+ export const cleanup = () => {
23
+ if (container === null)
24
+ return;
25
+ const isUnmounted = ReactDOM.unmountComponentAtNode(container);
26
+ if (isUnmounted) {
27
+ document.body.removeChild(container);
28
+ container = null;
29
+ }
30
+ };
@@ -1,9 +1,9 @@
1
- import type { ForwardRefExoticComponent, NamedExoticComponent, ComponentPropsWithoutRef, ComponentPropsWithRef, RefAttributes, ElementType } from 'react';
2
- export declare type ComponentRef<T extends ElementType> = T extends NamedExoticComponent<ComponentPropsWithoutRef<T> & RefAttributes<infer Method>> ? Method : ComponentPropsWithRef<T> extends RefAttributes<infer Method> ? Method : never;
3
- export declare type InferRef<T> = T extends ForwardRefExoticComponent<infer Ref> ? Ref extends React.RefAttributes<infer RefElement> ? RefElement : never : never;
4
- declare type ReactRefComponent<Props extends {
5
- ref?: React.Ref<any> | string;
6
- }> = (props: Props) => React.ReactNode;
7
- declare type ExtractRefAttributesRef<T> = T extends React.RefAttributes<infer P> ? P : never;
8
- export declare type GetRef<T extends ReactRefComponent<any> | React.Component<any>> = T extends React.Component<any> ? T : T extends React.ComponentType<infer P> ? ExtractRefAttributesRef<P> : never;
9
- export {};
1
+ import type { ForwardRefExoticComponent, NamedExoticComponent, ComponentPropsWithoutRef, ComponentPropsWithRef, RefAttributes, ElementType } from 'react';
2
+ export declare type ComponentRef<T extends ElementType> = T extends NamedExoticComponent<ComponentPropsWithoutRef<T> & RefAttributes<infer Method>> ? Method : ComponentPropsWithRef<T> extends RefAttributes<infer Method> ? Method : never;
3
+ export declare type InferRef<T> = T extends ForwardRefExoticComponent<infer Ref> ? Ref extends React.RefAttributes<infer RefElement> ? RefElement : never : never;
4
+ declare type ReactRefComponent<Props extends {
5
+ ref?: React.Ref<any> | string;
6
+ }> = (props: Props) => React.ReactNode;
7
+ declare type ExtractRefAttributesRef<T> = T extends React.RefAttributes<infer P> ? P : never;
8
+ export declare type GetRef<T extends ReactRefComponent<any> | React.Component<any>> = T extends React.Component<any> ? T : T extends React.ComponentType<infer P> ? ExtractRefAttributesRef<P> : never;
9
+ export {};
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -1,21 +1,21 @@
1
- export interface UseCopyToClipboardProps {
2
- /**
3
- * 重置复制成功状态延迟
4
- */
5
- timeout?: number;
6
- /**
7
- * @param copiedText 复制的文本
8
- * @returns
9
- */
10
- onCopy?: (copiedText: string) => void | undefined;
11
- /**
12
- *
13
- * @param error 复制错误对象
14
- * @returns
15
- */
16
- onError?: (error: Error) => void | undefined;
17
- }
18
- export declare function useCopyToClipboard(props?: UseCopyToClipboardProps): {
19
- isCopied: Boolean;
20
- copyToClipboard: (value: string) => void;
21
- };
1
+ export interface UseCopyToClipboardProps {
2
+ /**
3
+ * 重置复制成功状态延迟
4
+ */
5
+ timeout?: number;
6
+ /**
7
+ * @param copiedText 复制的文本
8
+ * @returns
9
+ */
10
+ onCopy?: (copiedText: string) => void | undefined;
11
+ /**
12
+ *
13
+ * @param error 复制错误对象
14
+ * @returns
15
+ */
16
+ onError?: (error: Error) => void | undefined;
17
+ }
18
+ export declare function useCopyToClipboard(props?: UseCopyToClipboardProps): {
19
+ isCopied: Boolean;
20
+ copyToClipboard: (value: string) => void;
21
+ };
@@ -1,44 +1,44 @@
1
- import { useCallback, useState } from 'react';
2
- import { useIsMounted } from './useIsMounted';
3
- export function useCopyToClipboard(props) {
4
- const { timeout = 2000, onCopy, onError } = props !== null && props !== void 0 ? props : {};
5
- const isMounted = useIsMounted();
6
- const [isCopied, setIsCopied] = useState(false);
7
- const copyToClipboard = useCallback((value) => {
8
- var _a;
9
- if (!isMounted())
10
- return;
11
- if (typeof window === 'undefined' || !((_a = navigator.clipboard) === null || _a === void 0 ? void 0 : _a.writeText)) {
12
- const error = new Error(`Cannot copy to clipboard, navigator.clipboard.writeText is not defined`);
13
- if (process.env.NODE_ENV === 'development')
14
- console.error(error);
15
- onError === null || onError === void 0 ? void 0 : onError(error);
16
- return;
17
- }
18
- if (typeof value !== 'string') {
19
- const error = new Error(`Cannot copy typeof ${typeof value} to clipboard, must be a string`);
20
- if (process.env.NODE_ENV === 'development')
21
- console.error(error);
22
- onError === null || onError === void 0 ? void 0 : onError(error);
23
- return;
24
- }
25
- if (value === '') {
26
- const error = new Error(`Cannot copy empty string to clipboard.`);
27
- if (process.env.NODE_ENV === 'development')
28
- console.error(error);
29
- onError === null || onError === void 0 ? void 0 : onError(error);
30
- return;
31
- }
32
- navigator.clipboard
33
- .writeText(value)
34
- .then(() => {
35
- setIsCopied(true);
36
- onCopy === null || onCopy === void 0 ? void 0 : onCopy(value);
37
- setTimeout(() => {
38
- setIsCopied(false);
39
- }, timeout);
40
- })
41
- .catch((error) => onError === null || onError === void 0 ? void 0 : onError(error));
42
- }, [onCopy, onError, timeout]);
43
- return { isCopied, copyToClipboard };
44
- }
1
+ import { useCallback, useState } from 'react';
2
+ import { useIsMounted } from './useIsMounted';
3
+ export function useCopyToClipboard(props) {
4
+ const { timeout = 2000, onCopy, onError } = props !== null && props !== void 0 ? props : {};
5
+ const isMounted = useIsMounted();
6
+ const [isCopied, setIsCopied] = useState(false);
7
+ const copyToClipboard = useCallback((value) => {
8
+ var _a;
9
+ if (!isMounted())
10
+ return;
11
+ if (typeof window === 'undefined' || !((_a = navigator.clipboard) === null || _a === void 0 ? void 0 : _a.writeText)) {
12
+ const error = new Error(`Cannot copy to clipboard, navigator.clipboard.writeText is not defined`);
13
+ if (process.env.NODE_ENV === 'development')
14
+ console.error(error);
15
+ onError === null || onError === void 0 ? void 0 : onError(error);
16
+ return;
17
+ }
18
+ if (typeof value !== 'string') {
19
+ const error = new Error(`Cannot copy typeof ${typeof value} to clipboard, must be a string`);
20
+ if (process.env.NODE_ENV === 'development')
21
+ console.error(error);
22
+ onError === null || onError === void 0 ? void 0 : onError(error);
23
+ return;
24
+ }
25
+ if (value === '') {
26
+ const error = new Error(`Cannot copy empty string to clipboard.`);
27
+ if (process.env.NODE_ENV === 'development')
28
+ console.error(error);
29
+ onError === null || onError === void 0 ? void 0 : onError(error);
30
+ return;
31
+ }
32
+ navigator.clipboard
33
+ .writeText(value)
34
+ .then(() => {
35
+ setIsCopied(true);
36
+ onCopy === null || onCopy === void 0 ? void 0 : onCopy(value);
37
+ setTimeout(() => {
38
+ setIsCopied(false);
39
+ }, timeout);
40
+ })
41
+ .catch((error) => onError === null || onError === void 0 ? void 0 : onError(error));
42
+ }, [onCopy, onError, timeout]);
43
+ return { isCopied, copyToClipboard };
44
+ }
@@ -1,2 +1,2 @@
1
- import { DependencyList } from 'react';
2
- export declare const useDeepCompareRef: (deps: DependencyList) => React.MutableRefObject<number>;
1
+ import { DependencyList } from 'react';
2
+ export declare const useDeepCompareRef: (deps: DependencyList) => React.MutableRefObject<number>;
@@ -1,11 +1,11 @@
1
- import { useRef } from 'react';
2
- import isEqual from 'react-fast-compare';
3
- export const useDeepCompareRef = (deps) => {
4
- const ref = useRef();
5
- const signalRef = useRef(0);
6
- if (deps === undefined || !isEqual(deps, ref.current)) {
7
- ref.current = deps;
8
- signalRef.current += 1;
9
- }
10
- return signalRef;
11
- };
1
+ import { useRef } from 'react';
2
+ import isEqual from 'react-fast-compare';
3
+ export const useDeepCompareRef = (deps) => {
4
+ const ref = useRef();
5
+ const signalRef = useRef(0);
6
+ if (deps === undefined || !isEqual(deps, ref.current)) {
7
+ ref.current = deps;
8
+ signalRef.current += 1;
9
+ }
10
+ return signalRef;
11
+ };
@@ -1,7 +1,7 @@
1
- declare type ContextMenuTarget = () => Element | Document | undefined | null;
2
- /**
3
- * 在HTML元素上禁用右键菜单
4
- * @param target 默认为 () => document
5
- */
6
- export declare const useDisableContextMenu: (target?: ContextMenuTarget) => void;
7
- export {};
1
+ declare type ContextMenuTarget = () => Element | Document | undefined | null;
2
+ /**
3
+ * 在HTML元素上禁用右键菜单
4
+ * @param target 默认为 () => document
5
+ */
6
+ export declare const useDisableContextMenu: (target?: ContextMenuTarget) => void;
7
+ export {};
@@ -1,24 +1,24 @@
1
- import { useEffect, useRef } from "react";
2
- const defaultContextMenuTarget = () => document;
3
- /**
4
- * 在HTML元素上禁用右键菜单
5
- * @param target 默认为 () => document
6
- */
7
- export const useDisableContextMenu = (target = defaultContextMenuTarget) => {
8
- const targetFn = useRef(target);
9
- targetFn.current = target;
10
- useEffect(() => {
11
- const el = targetFn.current();
12
- if (!el)
13
- return;
14
- const onContextMenu = (e) => {
15
- e.preventDefault();
16
- };
17
- el.addEventListener('contextmenu', onContextMenu);
18
- return () => {
19
- if (!el)
20
- return;
21
- el.removeEventListener('contextmenu', onContextMenu);
22
- };
23
- }, []);
24
- };
1
+ import { useEffect, useRef } from "react";
2
+ const defaultContextMenuTarget = () => document;
3
+ /**
4
+ * 在HTML元素上禁用右键菜单
5
+ * @param target 默认为 () => document
6
+ */
7
+ export const useDisableContextMenu = (target = defaultContextMenuTarget) => {
8
+ const targetFn = useRef(target);
9
+ targetFn.current = target;
10
+ useEffect(() => {
11
+ const el = targetFn.current();
12
+ if (!el)
13
+ return;
14
+ const onContextMenu = (e) => {
15
+ e.preventDefault();
16
+ };
17
+ el.addEventListener('contextmenu', onContextMenu);
18
+ return () => {
19
+ if (!el)
20
+ return;
21
+ el.removeEventListener('contextmenu', onContextMenu);
22
+ };
23
+ }, []);
24
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Alternative to useCallback that doesn't update the callback when dependencies change
3
+ *
4
+ * @see https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
5
+ * @see https://github.com/facebook/react/issues/14099#issuecomment-440013892
6
+ */
7
+ export declare const useEventCallback: <Args extends unknown[], Return>(fn: (...args: Args) => Return) => (...args: Args) => Return;
@@ -0,0 +1,19 @@
1
+ import * as React from 'react';
2
+ import { useCallback } from 'react';
3
+ // allow the hook to work in SSR
4
+ const useLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
5
+ /**
6
+ * Alternative to useCallback that doesn't update the callback when dependencies change
7
+ *
8
+ * @see https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
9
+ * @see https://github.com/facebook/react/issues/14099#issuecomment-440013892
10
+ */
11
+ export const useEventCallback = (fn) => {
12
+ const ref = React.useRef(() => {
13
+ throw new Error('Cannot call an event handler while rendering.');
14
+ });
15
+ useLayoutEffect(() => {
16
+ ref.current = fn;
17
+ });
18
+ return useCallback((...args) => ref.current(...args), []);
19
+ };
@@ -1,9 +1,9 @@
1
- import { ForwardedRef } from 'react';
2
- /**
3
- * https://stackoverflow.com/questions/66060217/i-cant-type-the-ref-correctly-using-useref-hook-in-typescript
4
- * 解决 Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void' TS类型错误
5
- * @param ref
6
- * @param initialValue
7
- * @returns
8
- */
9
- export declare const useForwardRef: <T>(ref: ForwardedRef<T>, initialValue?: any) => import("react").MutableRefObject<T>;
1
+ import { ForwardedRef } from 'react';
2
+ /**
3
+ * https://stackoverflow.com/questions/66060217/i-cant-type-the-ref-correctly-using-useref-hook-in-typescript
4
+ * 解决 Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void' TS类型错误
5
+ * @param ref
6
+ * @param initialValue
7
+ * @returns
8
+ */
9
+ export declare const useForwardRef: <T>(ref: ForwardedRef<T>, initialValue?: any) => import("react").MutableRefObject<T>;
@@ -1,22 +1,22 @@
1
- import { useEffect, useRef } from 'react';
2
- /**
3
- * https://stackoverflow.com/questions/66060217/i-cant-type-the-ref-correctly-using-useref-hook-in-typescript
4
- * 解决 Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void' TS类型错误
5
- * @param ref
6
- * @param initialValue
7
- * @returns
8
- */
9
- export const useForwardRef = (ref, initialValue = null) => {
10
- const targetRef = useRef(initialValue);
11
- useEffect(() => {
12
- if (!ref)
13
- return;
14
- if (typeof ref === 'function') {
15
- ref(targetRef.current);
16
- }
17
- else {
18
- ref.current = targetRef.current;
19
- }
20
- }, [ref]);
21
- return targetRef;
22
- };
1
+ import { useEffect, useRef } from 'react';
2
+ /**
3
+ * https://stackoverflow.com/questions/66060217/i-cant-type-the-ref-correctly-using-useref-hook-in-typescript
4
+ * 解决 Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void' TS类型错误
5
+ * @param ref
6
+ * @param initialValue
7
+ * @returns
8
+ */
9
+ export const useForwardRef = (ref, initialValue = null) => {
10
+ const targetRef = useRef(initialValue);
11
+ useEffect(() => {
12
+ if (!ref)
13
+ return;
14
+ if (typeof ref === 'function') {
15
+ ref(targetRef.current);
16
+ }
17
+ else {
18
+ ref.current = targetRef.current;
19
+ }
20
+ }, [ref]);
21
+ return targetRef;
22
+ };
@@ -0,0 +1 @@
1
+ export declare const useId: (id?: string | undefined) => string;
@@ -0,0 +1,12 @@
1
+ import { useMemo } from 'react';
2
+ function makeId() {
3
+ const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
4
+ let id = '';
5
+ for (let i = 0; i < 10; i += 1) {
6
+ id += chars[Math.floor(Math.random() * chars.length)];
7
+ }
8
+ return id;
9
+ }
10
+ export const useId = (id) => {
11
+ return useMemo(() => id !== null && id !== void 0 ? id : makeId(), [id]);
12
+ };
@@ -1,6 +1,6 @@
1
- /**
2
- * 使用 useIsFirstRender 区分第一次和后续渲染
3
- * useIsFirstRender 对于确定当前渲染是否是组件的第一个渲染很有用。当你想在初始渲染时有条件地执行某些逻辑或渲染特定组件时,这个钩子特别有价值,提供了一种有效的方法来区分第一次和后续渲染。
4
- * @returns boolean true 表示第一次渲染,false 表示其他渲染
5
- */
6
- export declare function useIsFirstRender(): boolean;
1
+ /**
2
+ * 使用 useIsFirstRender 区分第一次和后续渲染
3
+ * useIsFirstRender 对于确定当前渲染是否是组件的第一个渲染很有用。当你想在初始渲染时有条件地执行某些逻辑或渲染特定组件时,这个钩子特别有价值,提供了一种有效的方法来区分第一次和后续渲染。
4
+ * @returns boolean true 表示第一次渲染,false 表示其他渲染
5
+ */
6
+ export declare function useIsFirstRender(): boolean;
@@ -1,14 +1,14 @@
1
- import { useRef } from 'react';
2
- /**
3
- * 使用 useIsFirstRender 区分第一次和后续渲染
4
- * useIsFirstRender 对于确定当前渲染是否是组件的第一个渲染很有用。当你想在初始渲染时有条件地执行某些逻辑或渲染特定组件时,这个钩子特别有价值,提供了一种有效的方法来区分第一次和后续渲染。
5
- * @returns boolean true 表示第一次渲染,false 表示其他渲染
6
- */
7
- export function useIsFirstRender() {
8
- const renderRef = useRef(true);
9
- if (renderRef.current === true) {
10
- renderRef.current = false;
11
- return true;
12
- }
13
- return renderRef.current;
14
- }
1
+ import { useRef } from 'react';
2
+ /**
3
+ * 使用 useIsFirstRender 区分第一次和后续渲染
4
+ * useIsFirstRender 对于确定当前渲染是否是组件的第一个渲染很有用。当你想在初始渲染时有条件地执行某些逻辑或渲染特定组件时,这个钩子特别有价值,提供了一种有效的方法来区分第一次和后续渲染。
5
+ * @returns boolean true 表示第一次渲染,false 表示其他渲染
6
+ */
7
+ export function useIsFirstRender() {
8
+ const renderRef = useRef(true);
9
+ if (renderRef.current === true) {
10
+ renderRef.current = false;
11
+ return true;
12
+ }
13
+ return renderRef.current;
14
+ }
@@ -1,2 +1,2 @@
1
- /** 用于判断组件是否挂载 */
2
- export declare function useIsMounted(): () => boolean;
1
+ /** 用于判断组件是否挂载 */
2
+ export declare function useIsMounted(): () => boolean;
@@ -1,13 +1,13 @@
1
- import { useCallback, useEffect, useRef } from 'react';
2
- /** 用于判断组件是否挂载 */
3
- export function useIsMounted() {
4
- const mountedRef = useRef(false);
5
- const get = useCallback(() => mountedRef.current, []);
6
- useEffect(() => {
7
- mountedRef.current = true;
8
- return () => {
9
- mountedRef.current = false;
10
- };
11
- }, []);
12
- return get;
13
- }
1
+ import { useCallback, useEffect, useRef } from 'react';
2
+ /** 用于判断组件是否挂载 */
3
+ export function useIsMounted() {
4
+ const mountedRef = useRef(false);
5
+ const get = useCallback(() => mountedRef.current, []);
6
+ useEffect(() => {
7
+ mountedRef.current = true;
8
+ return () => {
9
+ mountedRef.current = false;
10
+ };
11
+ }, []);
12
+ return get;
13
+ }
@@ -1,2 +1,2 @@
1
- import { useEffect } from 'react';
2
- export declare const useIsomorphicLayoutEffect: typeof useEffect;
1
+ import { useEffect } from 'react';
2
+ export declare const useIsomorphicLayoutEffect: typeof useEffect;
@@ -1,2 +1,2 @@
1
- import { useEffect, useLayoutEffect } from 'react';
2
- export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
1
+ import { useEffect, useLayoutEffect } from 'react';
2
+ export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
@@ -1,14 +1,14 @@
1
- declare type UseMediaQueryOptions = {
2
- /**
3
- * The default value to return if the hook is being run on the server.
4
- * @default false
5
- */
6
- defaultValue?: boolean;
7
- /**
8
- * If `true` (default), the hook will initialize reading the media query. In SSR, you should set it to `false`, returning `options.defaultValue` or `false` initially.
9
- * @default true
10
- */
11
- initializeWithValue?: boolean;
12
- };
13
- export declare function useMediaQuery(query: string, { defaultValue, initializeWithValue }?: UseMediaQueryOptions): boolean;
14
- export {};
1
+ declare type UseMediaQueryOptions = {
2
+ /**
3
+ * The default value to return if the hook is being run on the server.
4
+ * @default false
5
+ */
6
+ defaultValue?: boolean;
7
+ /**
8
+ * If `true` (default), the hook will initialize reading the media query. In SSR, you should set it to `false`, returning `options.defaultValue` or `false` initially.
9
+ * @default true
10
+ */
11
+ initializeWithValue?: boolean;
12
+ };
13
+ export declare function useMediaQuery(query: string, { defaultValue, initializeWithValue }?: UseMediaQueryOptions): boolean;
14
+ export {};