xxf_react 0.3.1 → 0.3.3

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.
@@ -1,3 +1,4 @@
1
1
  export * from './scroll-to-center';
2
2
  export * from './tailwind';
3
+ export * from './reload';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA,cAAc,oBAAoB,CAAA;AAClC,cAAc,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA,cAAc,oBAAoB,CAAA;AAClC,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA"}
@@ -1,3 +1,4 @@
1
1
  'use client';
2
2
  export * from './scroll-to-center';
3
3
  export * from './tailwind';
4
+ export * from './reload';
@@ -0,0 +1,62 @@
1
+ /**
2
+ * useReloadKey 配置选项
3
+ */
4
+ interface UseReloadKeyOptions {
5
+ /** 初始值,默认 0 */
6
+ initialValue?: number;
7
+ /** reload 后的回调,参数为当前累计刷新次数 */
8
+ onReload?: (reloadCount: number) => void;
9
+ }
10
+ /**
11
+ * useReloadKey 返回值
12
+ */
13
+ interface UseReloadKeyReturn {
14
+ /** 当前 reloadKey 值,用于组件 key 属性 */
15
+ reloadKey: number;
16
+ /** 触发刷新,reloadKey 自增 1 */
17
+ reload: () => void;
18
+ /** 重置为初始值,同时清零刷新次数 */
19
+ reset: () => void;
20
+ /** 累计刷新次数(不受 reset 影响前的总次数) */
21
+ reloadCount: number;
22
+ }
23
+ /**
24
+ * 用于强制刷新组件的 Hook
25
+ *
26
+ * 通过改变 key 值来触发 React 组件的卸载和重新挂载,
27
+ * 常用于需要完全重置组件状态的场景。
28
+ *
29
+ * @param options - 配置选项
30
+ * @returns 包含 reloadKey、reload、reset、reloadCount 的对象
31
+ *
32
+ * @example
33
+ * ```tsx
34
+ * // 基础用法
35
+ * const { reloadKey, reload } = useReloadKey();
36
+ *
37
+ * return (
38
+ * <div>
39
+ * <ChildComponent key={reloadKey} />
40
+ * <button onClick={reload}>刷新组件</button>
41
+ * </div>
42
+ * );
43
+ * ```
44
+ *
45
+ * @example
46
+ * ```tsx
47
+ * // 带回调的用法
48
+ * const { reloadKey, reload, reloadCount } = useReloadKey({
49
+ * onReload: (count) => console.log(`第 ${count} 次刷新`)
50
+ * });
51
+ *
52
+ * return (
53
+ * <div>
54
+ * <DataFetcher key={reloadKey} />
55
+ * <button onClick={reload}>重新加载数据 (已刷新 {reloadCount} 次)</button>
56
+ * </div>
57
+ * );
58
+ * ```
59
+ */
60
+ export declare function useReloadKey(options?: UseReloadKeyOptions): UseReloadKeyReturn;
61
+ export {};
62
+ //# sourceMappingURL=reload.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reload.d.ts","sourceRoot":"","sources":["../../src/utils/reload.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,UAAU,mBAAmB;IACzB,eAAe;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,8BAA8B;IAC9B,QAAQ,CAAC,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,IAAI,CAAC;CAC5C;AAED;;GAEG;AACH,UAAU,kBAAkB;IACxB,iCAAiC;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,0BAA0B;IAC1B,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,sBAAsB;IACtB,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,+BAA+B;IAC/B,WAAW,EAAE,MAAM,CAAC;CACvB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,kBAAkB,CA+BlF"}
@@ -0,0 +1,66 @@
1
+ import { useState, useCallback, useRef } from 'react';
2
+ /**
3
+ * 用于强制刷新组件的 Hook
4
+ *
5
+ * 通过改变 key 值来触发 React 组件的卸载和重新挂载,
6
+ * 常用于需要完全重置组件状态的场景。
7
+ *
8
+ * @param options - 配置选项
9
+ * @returns 包含 reloadKey、reload、reset、reloadCount 的对象
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * // 基础用法
14
+ * const { reloadKey, reload } = useReloadKey();
15
+ *
16
+ * return (
17
+ * <div>
18
+ * <ChildComponent key={reloadKey} />
19
+ * <button onClick={reload}>刷新组件</button>
20
+ * </div>
21
+ * );
22
+ * ```
23
+ *
24
+ * @example
25
+ * ```tsx
26
+ * // 带回调的用法
27
+ * const { reloadKey, reload, reloadCount } = useReloadKey({
28
+ * onReload: (count) => console.log(`第 ${count} 次刷新`)
29
+ * });
30
+ *
31
+ * return (
32
+ * <div>
33
+ * <DataFetcher key={reloadKey} />
34
+ * <button onClick={reload}>重新加载数据 (已刷新 {reloadCount} 次)</button>
35
+ * </div>
36
+ * );
37
+ * ```
38
+ */
39
+ export function useReloadKey(options = {}) {
40
+ const { initialValue = 0, onReload } = options;
41
+ const [reloadKey, setReloadKey] = useState(initialValue);
42
+ const [reloadCount, setReloadCount] = useState(0);
43
+ // 用 ref 存储回调,避免 useCallback 频繁重建
44
+ const onReloadRef = useRef(onReload);
45
+ onReloadRef.current = onReload;
46
+ const reload = useCallback(() => {
47
+ setReloadKey(prev => prev + 1);
48
+ setReloadCount(prev => {
49
+ var _a;
50
+ const newCount = prev + 1;
51
+ // 回调放在 setState 外部更规范,但这里为了获取最新值
52
+ (_a = onReloadRef.current) === null || _a === void 0 ? void 0 : _a.call(onReloadRef, newCount);
53
+ return newCount;
54
+ });
55
+ }, []);
56
+ const reset = useCallback(() => {
57
+ setReloadKey(initialValue);
58
+ setReloadCount(0);
59
+ }, [initialValue]);
60
+ return {
61
+ reloadKey,
62
+ reload,
63
+ reset,
64
+ reloadCount,
65
+ };
66
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xxf_react",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "private": false,
5
5
 
6
6
  "type": "module",