iframe-micro-react-state
Version:
基于 iframe 的微应用状态管理与父应用通信工具,仅面向 React 项目生态。
63 lines • 1.58 kB
text/typescript
//#region src/types.d.ts
/**
* 父应用传递的数据类型定义
*/
interface MicroAppData {
/** 用户信息 */
userInfo?: {
id?: number | string;
name?: string;
role?: string;
permissions?: string[];
[key: string]: any;
};
/** 菜单列表 */
menuList?: Array<{
label?: string;
path?: string;
icon?: string;
children?: any[];
[key: string]: any;
}>;
/** 权限路由 */
permissionRoutes?: string[];
/** 权限列表 */
authorityList?: string[];
/** 主题设置 */
theme?: 'light' | 'dark';
/** 自定义数据 */
customData?: Record<string, any>;
}
/**
* 微应用初始化状态
*/
interface MicroAppInitState {
/** 是否已初始化 */
initialized: boolean;
/** 初始化时间 */
initTime?: number;
/** 接收到的数据 */
data?: MicroAppData;
/** 错误信息 */
error?: string;
}
/**
* 消息事件类型
*/
//#endregion
//#region src/react.d.ts
/**
* React Hook:iframe 子页面获取父页面下发的微应用状态(唯一推荐用法)
* - 自动监听/解绑 postMessage
* - 状态自动响应式更新
* - 类型安全,极简
*/
declare function useMicroAppState(): MicroAppInitState | undefined;
/**
* 权限检查 Hook
* @param codes string | string[] 需要检查的权限 code 或 code 数组
* @returns boolean 是否有权限(任意一个 code 匹配即为 true)
*/
declare function useAuthorityChecker(codes: string | string[]): boolean;
//#endregion
export { type MicroAppData, type MicroAppInitState, useAuthorityChecker, useMicroAppState };