reactjs - 如何在 Typescript 中输入匹配项?
问题描述
我有一个必须使用 match.params 的组件,但是当我尝试它时出现错误。在我的情况下如何获得匹配参数?
零件
type MainInfoProps = {
mainInfo: IUser,
boxShadow?: number,
};
export const UserCard: React.FC<MainInfoProps> = (props) => {
return()
}
错误
Property 'match' does not exist on type 'PropsWithChildren<MainInfoProps>'
解决方案
您应该使用RouteComponentProps
which 是接受params对象类型的泛型类型。
import { RouteComponentProps } from 'react-router-dom';
type MainInfoProps = {
mainInfo: IUser;
boxShadow?: number;
} & RouteComponentProps<{ paramOne: string; paramTwo: string }>;
然后你就可以使用它了:
props.match.params.paramOne;
推荐阅读
- c - 在 C 中定义不等间隔的点
- css - 在 CSS 规则内导入以在同一节点级别附加类
- javascript - 在“汉谟拉比”程序中找不到问题,从书中复制(德语)
- regex - 尝试从 Perl 中的文本文件中提取唯一字符串
- list - 如何根据特定要求创建用户名列表?(Android Studio - Azure)
- regex - stuby 没有返回对我的请求的响应,其中包含 % 的查询参数
- wso2 - 在登录应用程序期间面临问题并收到与 WSO2 相关的错误
- javascript - 如何告诉 JSDoc 某个函数是函数定义类的方法?
- visual-studio - 无法导入数据库 - “Guid 应包含 32 位数字和 4 个破折号”错误
- git - 如何镜像两个 Azure DevOps/VSTS 存储库之间的特定分支