简体   繁体   English

在动态组件上反应 Typescript

[英]React Typescript on dynamic component

Using React with typescript I want to be able to create a dynamic component.将 React 与 typescript 一起使用,我希望能够创建一个动态组件。 Here is snapshot from code这是来自代码的快照

const CustomTag = `${section.header_type}`;
<CustomTag className={styles.textimage__title}>{section?.header}</CustomTag>

on CustomTag markup in VSCode I get the following error在 VSCode 中的 CustomTag 标记上我收到以下错误

Type '{ children: string; className: string; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'children' does not exist on type 'IntrinsicAttributes'.ts

How do I define in this case for this component properly the types?在这种情况下,如何正确定义此组件的类型?

Declare CustomTag type as being keyof JSX.IntrinsicElements .CustomTag类型声明为keyof JSX.IntrinsicElements的 key。 It's going to be like the following code snippet.它将类似于以下代码片段。

const CustomTag: keyof JSX.IntrinsicElements = `${section.header_type}`;
<CustomTag className={styles.textimage__title}>{section?.header}</CustomTag>

Just note that the ${section.header_type} should be a valid HTML tag.请注意${section.header_type}应该是有效的 HTML 标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM