typescript - "{text, comp} 的打字稿含义:NavProps & Comp“?
问题描述
我是打字稿的新手,遇到了一些类似下面的代码。
export interface Comp<T> {
comp: (props: T) => JSX.Element;
}
export interface NavProps {
text: string;
}
export default function Nav({text, comp}: NavProps & Comp<NavProps>) {
let renderProps: NavProps = {
text: text,
};
return comp(renderProps);
}
一些命名可能会令人困惑,但我不想弄乱工作代码。
这些代码打算做什么?特别是,这是什么?{text, comp}: NavProps & Comp<NavProps>
解决方案
这段代码通常看起来像一个React组件。
{text, comp}: NavProps & Comp<NavProps>
正在声明函数的参数,但使用解构。
基本上
export default function Nav({text, comp}: NavProps & Comp<NavProps>) {
let renderProps: NavProps = {
text: text,
};
return comp(renderProps);
}
将与以下内容相同:
export default function Nav(props: NavProps & Comp<NavProps>) {
let renderProps: NavProps = {
text: props.text,
};
return props.comp(renderProps);
}
推荐阅读
- amazon-web-services - 让 AWS AppSync 与 CloudFront 和自定义域名一起使用
- ruby-on-rails - 如何在 Ruby on Rails 中查询 Spamhaus DBL?
- jmeter - 如何对并发用户数有限制的第 3 方 API 进行性能测试?
- sql - 在关系数据库中表示对称关系的更好方法?
- javascript - JavaScript/JQuery - 使用 blur() 事件检测 Shift + Tab
- apache-kafka - 卡夫卡事务生产者
- java - 从 .csv 文件加载标记时如何获取标记的位置?
- php - 将字符串数组添加到逗号分隔的单词
- r - Gsub R中关键字后的每个元素
- c# - Entity Framework 使用 Oracle 命令 Add-Migration 在十进制类型的表键字段中创建,但在模板中是整数