javascript - 尝试动态呈现 JSX 标签
问题描述
我正在创建一个<Text />
组件,以便我可以轻松控制 Text 在我的应用程序中的使用方式。我希望能够为<Text />
组件选择一个标签,具体取决于它是什么(例如<p>
,对于正文文本,<h1>
对于标题)。
但是,我被困在第一步。当我尝试使用函数返回标签时,出现以下错误:
Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes'.ts(2559)
这是我的组件:
import * as React from 'react'
export class Text extends React.Component {
constructor(props) {
super(props)
}
getMarkup() {
return 'h1'
}
render() {
const CustomTag = this.getMarkup()
return (<CustomTag>Hello</CustomTag>)
}
}
解决方案
由于您要将标签视为字符串,因此可以使用React.createElement
. 里面render
,写如下:
const element = React.createElement(this.getMarkup(), {}, 'Hello');
return <>{element}</>;
基本上,createElement
期望元素的类型为字符串,因此您可以传递 ,'h1'
而不会让 TypeScript 打扰您。
此外,您可以看到我传递了一个空数组作为第二个参数:在那里您可以传递任何道具,例如style
, onClick
, ... 通常,在这种情况下,您可以编写如下:
const element = React.createElement(this.getMarkup(), {{...this.props}}, 'Hello');
但当然,您需要使用..在Text
道具中添加正确的类型,如下所示:React.HTMLProps<T>
class App extends React.Component<React.HTMLProps<HTMLHeadingElement | HTMLParagraphElement>, IState> {`
在这种情况下,我只考虑h
andp
元素。
编辑:如果您要将HTMLProps
与您自己的道具结合起来,例如IProps
,那么您将编写IProps & React.HTMLProps<HTMLHeadingElement | HTMLParagraphElement>
.
此时,this.props
您将在内部拥有 ( p
| h
) 道具和在IProps
.
然后,在这一点上,由于p
and h
element 不应该接受来自 的道具IProps
,你应该重写createElement
如下:
// Assuming you have this IProps
interface IProps {
tag: string;
myProp: number;
}
// Inside render
const { tag, myProp, ...otherProps } = {...this.props};
const element = React.createElement(this.getMarkup(), otherProps, 'Hello');
return <>{element}</>;
推荐阅读
- jq - 如何将两个 json 与 jq 相乘?
- r - R中的带状变量
- google-analytics - 如何从 URL 中删除令牌
- flutter - 如何使用颤振驱动器找到关闭按钮
- php - 在大文本中搜索关键字并使用关键字 php 获取部分文本
- c# - C#,SQL Server:四舍五入
- css - Mozilla firefox中的宽度错误[带有引导程序的站点]
- postgresql - 在 PostgreSQL 中使用 ltree 获取两条路径之间的节点
- oracle - 如何实现@SqlCall调用存储过程
- javascript - JSON.parse(JSON.stringify(e)) converts object to a string