React关闭缓存标签页刷新页面的hooks

在后台系统中有Form单页面时,执行了新增或者编辑完都要求关闭当前页回到列表页并执行刷新

用了hooks实现后,记录下来方便后续也给大伙分享,这个并没过多的复杂逻辑,需要用时直接CV就好

/** 复制代码
 * @hook useCloseTabPage: 用于关闭当前刷新页面
 * */

type closeCurPageAndReloadAppointPage = (reloadPage?: string) => void
type refreshAppointPage = ((path: string) => void);
type dropScope = (name: string | RegExp) => Promise<boolean>
type closeCurPageAndJumpToNewPage = (reloadPage?: string, newPage?: string) => void


type CloseProps = [
    closeCurPageAndReloadAppointPage,
    refreshAppointPage,
    dropScope,
    closeCurPageAndJumpToNewPage
]

const useCloseTabPage = (): CloseProps=> {
    const { dropScope, refreshScope } = useAliveController();
    // 刷新指定页面
    const refreshAppointPage = (path: string) => {
        if (path) {
            refreshScope(path);
        }
    };
    // 关闭当前页面
    const closeCurPageAndReloadAppointPage = (reloadPage?: string) => {
        if (reloadPage) {
            refreshScope(reloadPage);
        }
        dropScope(window.location?.hash?.slice(1));
        history.goBack();
    };
    //关闭当前页面并跳转新页面
    const closeCurPageAndJumpToNewPage = (reloadPage?: string, newPage?: string) => {
        if (reloadPage) {
            refreshScope(reloadPage);
        }
        dropScope(window.location?.hash?.slice(1));
        if (newPage) {
            history.push({
                pathname: newPage,
            });
        }
    };
    return [closeCurPageAndReloadAppointPage, refreshAppointPage, dropScope, closeCurPageAndJumpToNewPage];
};

export default useCloseTabPage;

引入hooks

import useCloseTabPage from '@/app/hooks/useCloseTabPage';

顶部声明变量

const [refreshAppointPage] = useCloseTabPage();

最后调用即可

refreshAppointPage('/rbacRole/rbacRoleList');

最后愿大家都能快速CV,提升效率,增加摸鱼时间~~~

相关推荐
低代码布道师1 天前
第06篇:用户新增——React Hook Form + Zod 搭建可复用的表单体系
前端·react.js·前端框架
打工仔折腾 AI1 天前
智能体开发实战:Agent架构拆解与ReAct执行循环工程落地
前端·后端·python·react.js·架构·langchain·ai agent 实战
少年的范儿1 天前
React Native + Expo 实战 14|管理远端列表:缓存、刷新与分页
react native·react.js·缓存
少年的范儿1 天前
React Native + Expo 实战 13|实现登录:会话恢复、过期处理与账号隔离
javascript·react native·react.js
码哥字节2 天前
Remotion Skills:用 React 代码做视频,简直太爽了
react.js
GuWenyue3 天前
我 push 了三个元素,React 说数组没变:把「不可变更新」的 6 个反例全写了一遍
前端·javascript·react.js
code2cat3 天前
React Native页面导航:编辑任务后,详情页为什么能显示新标题
javascript·react native·react.js
zach4 天前
吃透 React setState 同步异步之谜:从批量更新、事件循环到版本演进的完整拆解
前端·react.js
用户298698530144 天前
在 React 中实现 Excel 与 TXT 格式互转的前端方案
javascript·react.js·excel
code2cat5 天前
React Native入门:用Expo在手机上录入第一条任务
react native·react.js·智能手机