reactjs - 将组件作为构造函数传递时如何键入组件?
问题描述
我正在尝试将组件作为构造函数传递,但 Typescript 抱怨我的接口,因为我猜我正在扩展 IpanelContent。
请参阅下面的所有代码,但我的问题是;component
IpanelType应该是什么类型?
我希望能够创建几个扩展 IpanelContent 的新接口,而无需更改 IPanelType 接口
我的实现:
<PanelStack
visible={visibleMenu}
onVisibleChange={(visible: boolean) => {
setVisibleMenu(visible);
}}
options={{
api:
'https://cors-anywhere.herokuapp.com/https://test-www.regionhalland.se/app/plugins/region-halland-plugin-navigation/ajax/region_halland_navigation.php',
}}
initialType="panelMeny"
types={[
{
name: 'panelMenu',
component: PanelMenu,
options: {
someProp: 'SomeProp',
},
},
]}
></PanelStack>
我的界面:
export interface IPanelContent {
navigation: IPanelStackNavigation;
isCurrent: boolean;
}
export interface IPanelType {
name: string;
component: ComponentType<IPanelContent>;
options?: IPanelOptions;
}
export interface IPanelMenu extends IPanelContent {
api: string;
id?: string | number;
}
export interface IPanelImage extends IPanelContent {
url: string;
}
我的错误:
Type '({ navigation, id, api, isCurrent }: IPanelMenu) => ReactElement<IPanelContent>' is not assignable to type 'ComponentType<IPanelContent>'.
Type '({ navigation, id, api, isCurrent }: IPanelMenu) => ReactElement<IPanelContent>' is not assignable to type 'FunctionComponent<IPanelContent>'.
Types of parameters '__0' and 'props' are incompatible.
Property 'api' is missing in type 'PropsWithChildren<IPanelContent>' but required in type 'IPanelMenu'.ts(2322)
解决方案
推荐阅读
- php - 如何在 PHP 中使用数组制作表格?
- tmux - 将 tmux 选择传递给 shell 命令?
- mysql - MySQL:如何将表 A 的数据复制到新的不存在的表 B?
- java - 我正在尝试构建一个跟踪方法,以 2dArray 的形式跟踪图片轮廓的坐标
- visual-studio-2017 - 在 TFS 构建过程之后,未创建发布文件夹
- reactjs - 使用带有 React 和 MobX 的 ES6 Map 不会在所有更改上重新渲染
- javascript - Jupyter Lab 隐藏垂直导航窗格
- python - 通知发送在使用 sudo 运行的 python 脚本中不起作用
- c++ - std::将数据移出 Eigen::Matrix
- c# - C#解析文件末尾缺少换行符的文件