记录一下碰到的可复用的方法
获取url指定pathnmae后面的参数
形如http://localhost:3000/projects/1/kanban
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| // 根据url后面的参数去请求, hook获取id export const useProjectIdInUrl = () => { const { pathname } = useLocation(); const id = pathname.match(/projects\/(\d+)/)?.[1]; // ["projects/1", "1", index: 1, input: "/projects/1/kanban", groups: undefined] [1] return Number(id); }; // 最后得到1
// 请求数据 export const useProjectInUrl = () => useProjectById(useProjectIdInUrl());
// 获取params export const useKanbanSearchParams = () => ({ projectId: useProjectIdInUrl() });
// 获取QueryKe export const useKanBansQueryKey = () => ["kanbans", useKanbanSearchParams()];
|