useMatch 如何使用-React Router
作者: 分类:编程技术浏览(1,574)2023-5-18

useMatch 返回传入 path 的匹配信息;如果匹配失败将返回 null

类型定义如下:

declare function useMatch(pattern: {
  path: string;
  caseSensitive?: boolean;
  end?: boolean;
} | string): {
  params: Record<string, string>;
  pathname: string;
  pattern: {
    path: string;
    caseSensitive?: boolean;
    end?: boolean;
  };
};

示例:

import { useMatch } from 'umi';
 
// when url = '/events/12'
const match = useMatch('/events/:eventId');
console.log(match?.pathname, match?.params.eventId);
// '/events/12 12'