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,提升效率,增加摸鱼时间~~~

相关推荐
Seven_Ting6 小时前
React-Hooks笔记
前端·笔记·react.js
Revolution6118 小时前
React 组件重新渲染时,到底重新执行了什么
前端·react.js·面试
林焱_RPAAI18 小时前
影刀RPA技术深度:CSS选择器高级实战指南——伪类属性选择器与性能对比完全解析
vue.js·react.js
名字还没想好☜1 天前
React 受控输入框光标跳到末尾:格式化输入时的 selection 丢失 bug 与修复
前端·javascript·react.js·bug·react·next.js
10share1 天前
React 新一代样式隔离方案 —— 编译时、零运行时、原生写法
前端·react.js
光影少年1 天前
RN 的EventEmitter 双向通信
前端·react native·react.js
GuWenyue2 天前
90%前端写React+TS都踩坑!从组件类型、单向数据流到本地存储完整实战
前端·react.js
张元清2 天前
React useThrottle Hook:节流值与回调(2026)
javascript·react.js
烬羽2 天前
React 状态提升:一个 ColorPicker,彻底搞懂状态该放哪里
react.js·typescript·设计
console.log('npc')2 天前
React 跨项目集成实战:iframe 实现子项目详情弹窗
前端·javascript·react.js