如何将匹配道具添加到打字稿中的接口
如何将匹配道具添加到打字稿中的接口
嗨,我的打字稿完全陌生,这让我有些困惑。我应该如何从我在自定义组件中作为props接收的react-router match定义props?
我的组件代码:
type IProps = {
match:________, //WHAT SHOULD I PUT HERE?
}
type Istate = {
loading: boolean,
data: Object,
}
export class SubjectDashboard extends Component<Iprops, Istate> {
componentDidMount() {
this.getSubjectResults().then((r) => this.setState({loading: false, data: r}));
}
async getSubjectResults() {
const roleId = this.context.role.id;
const subjectId = this.props.match.params.id;
const response = await fetch(RequestsUrlConstants.getStudentSubjectTreeWithResults(roleId, subjectId));
const data = await response.json();
if (response.status !== 200) {
toast.error('Nepodarilo sa načítať dáta');
return;
}
console.log(data);
return data;
}
}
回答如下:首先,看起来您有一个Iprops
和IProps
的错字
接下来,您可以使用界面:
interface IParams {
id: string
}
interface IMatch {
params: IParams
}
interface IProps {
match: IMatch
}
或类型:
type IParams = {
id: string
}
type IMatch = {
params: IParams
}
type IProps = {
match: IMatch
}
或内联:
interface IProps {
match: {
params: {
id: string
}
}
}
最新文章
- 南宁市计算机技术专业学校,南宁电脑技术学校有哪些
- Driller源码阅读笔记(二)
- springboot项目搭建0000
- Cocos2dx 之 cocosbuilder的使用
- WinRAR 3.51 注册码
- tensorflow安装过程
- 调试之DUMP文件生成和使用
- java文件下载错误提示
- 清华应届程序员同时被5家公司录取,晒出工资,网友:羡慕
- gallery3d的源码分析——入口
- [SOA介绍]什么是SOA?
- VSS2005使用技巧
- WindowsForm 入门
- C++实现matlab中的interp1和interp2插值
- 使用nano编辑器进行查找和替换
- 统计学,机器学习,深度学习,数据挖掘的联系
- DBCS