reactjs - 有没有办法通过道具传递接口?
问题描述
我想知道是否有办法通过道具传递接口。
例如,下面的组件尝试将接口传递给 Apple 组件。
import Apple from "./";
Interface A {
name: string;
}
const Component = () => {
return (
<Apple
...
type={A} <--- Pass it in like this
/>
);
}
1-以上可以做到吗?
2-如果是这样,Apple组件内的接口类型是什么?
解决方案
你不能通过 props 传递接口,因为它只存在于构建时,而不是运行时。如果你想在某个地方打发时间,你可以使用泛型。
export interface AppleProps<T> { /* ... */ }
export function Apple<T>(props: AppleProps<T>) { /* ... */ }
// ...
import Apple from "./";
interface A {
name: string;
}
const Component = () => {
return (
// You can provide generic arguments for react elements
<Apple<A> />
);
}
推荐阅读
- python - 如何根据特定数字分隔字母?
- azure-functions - 从应用服务计划切换到 Azure Functions 高级计划
- compilation - 我如何确定为什么代码编译与 mpif90 一起挂起?
- svg - THREE.ExtrudeBufferGeometry 的多边形很少
- r - 将显着性星添加到优势比图 (ggplot)
- python - python sqlalchemy原始查询非常慢
- python - 无法将字符串转换为浮点数:'100.0000%'
- c++ - 使用 std::thread 时与 g++/OpenMP 相关的错误?
- javascript - 将值添加到队列并在显示它们之前等待
- reactjs - 如何在地图上显示3s平面中的一条线?