Reactjs 返回React节点的TypeScript方法
我正在尝试用TypeScript编写一个方法,该方法返回一个Reactjs 返回React节点的TypeScript方法,reactjs,typescript,Reactjs,Typescript,我正在尝试用TypeScript编写一个方法,该方法返回一个ReactNode: const getNode = (Tag: string, children: string): React.ReactNode => { return <Tag>{children}</Tag>; }; 如何修复此错误?我建议在这种情况下避免使用JSX,直接使用React.createElement(标记,{},子元素)我建议在这种情况下避免使用JSX,直接使用React.
ReactNode
:
const getNode = (Tag: string, children: string): React.ReactNode => {
return <Tag>{children}</Tag>;
};
如何修复此错误?我建议在这种情况下避免使用JSX,直接使用
React.createElement(标记,{},子元素)代码>我建议在这种情况下避免使用JSX,直接使用React.createElement(标记,{},子元素)
为什么不使用React.createElement()
?为什么不使用React.createElement()
?
getNode('p', 'Hello World'); // returns <p>Hello World</p>
getNode('h1', 'Title'); // returns <h1>Title</h1>
TS2559: Type { children: string; } has no properties in common with 'IntrinsicAttributes'.