javascript - React Pass Component 作为 props 并渲染它
问题描述
我正在尝试渲染作为道具传递的 React 组件。
export type SummaryCardProps = {
site?: string;
title: string;
description?: string;
image?: URL;
imageAlt?: string;
ContainerElement: Component;
};
export const SummaryCard: FC<SummaryCardProps> = (props) => {
return (
<props.ContainerElement>
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content={new URL('https://' + props.site).toString()} />
<meta name="twitter:title" content={`${props.title}`} />
<meta name="twitter:description" content="View the album on Flickr." />
<meta name="twitter:image" content="https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg" />
</props.ContainerElement>
);
};
代码用于@peuconomia/react-meta-tags
. 现在,我正在尝试使其通用。我希望 ContainerElelment 成为任何 ReactComponent 以保持简单明了。我需要它用于任何一个<Head />
(Next.js)或<Fragment />
现在。
可能的答案:渲染一个反应组件。但是,当我将 Component 作为类型时,它给了我错误。
TS2604: JSX element type 'props.ContainerElement' does not have any construct or call signatures
解决方案
将 ContainerElement 的类型更改为React.ReactNode
推荐阅读
- javascript - React.js 到痛苦的 HTML:如何将 imageupload.html 反应代码转换为 Angular 6 HTML 代码?
- java - 用 Node 和应用程序编写的测试框架的代码覆盖率是用 Java 编写的
- javascript - “从不”类型上不存在属性“isFetching”。使用 mapStateToProps
- c# - 静态文件不通过 IIS 提供,但通过 localhost 提供 - ASP.NET Core 3.1 MVC & C#
- node.js - 找不到 Promise 回调
- c# - Xamarin.Forms 标签中的超链接
- python - 函数之间的全局变量总是=无python
- ruby-on-rails - 为什么 RubyMine 控制台无法启动?
- angular - 如何根据点击上一页显示不同的图像
- gstreamer - gstreamer:Demux & Remux MKV,保存视频