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

相关推荐
TheRouter1 小时前
AI Agent 开发中的模型调度策略:何时用便宜模型,何时用强模型
前端·人工智能·react.js
ZHENGZJM2 小时前
前端基石:React + Vite + TypeScript 项目搭建
前端·react.js·typescript
ZHENGZJM2 小时前
认证增强:图形验证码、邮箱验证与账户安全
安全·react.js·go·gin
光影少年2 小时前
RN长列表(FlatList)性能优化的具体手段有哪些?
react native·react.js·性能优化
阿捞23 小时前
python-langchain框架(3-20-智能问答ZeroShot_ReAct Agent 从零搭建)
python·react.js·langchain
Lee川17 小时前
从零构建现代化登录界面:React + Tailwind CSS 前端工程实践
前端·react.js
Embrace92419 小时前
React Native + Realm 离线方案处理
javascript·react native·react.js·realm
TheRouter21 小时前
构建一个支持多模型的 AI 聊天应用:React + TheRouter API 全栈教程
前端·人工智能·react.js
yuki_uix21 小时前
面试题里的 Custom Hook 思维:从三道题总结「异步状态管理」通用模式
前端·react.js·面试
Cxiaomu1 天前
React Native 双端一体工程,如何实现分端运行与分端打包?
javascript·react native·react.js