【发布时间】:2021-12-27 17:18:15
【问题描述】:
为什么 react router v6 useParams 会返回属性可能为“未定义”的对象?
在下面的代码中,我的 IDE 指示 const slug: string | undefined。
const { slug } = useParams<"slug">();
/**
* The parameters that were parsed from the URL path.
*/
export type Params<Key extends string = string> = {
readonly [key in Key]: string | undefined;
};
但是为什么Params 不是这样定义的(没有| undefined):
export type Params<Key extends string = string> = {
readonly [key in Key]: string;
};
如果带有参数的路由与 URL 匹配,则该参数不能是 undefined。那么,useParams 是否会返回 undefined 参数?
【问题讨论】:
标签: typescript react-router-dom