我正在尝试将此简单的div
添加到.tsx文件的返回块中:
<div id="bizible.reportUser" style="display:none" data-email="some@email.com"/>
我通过以下方式进行操作:
import React from 'react';
import Radium from 'radium';
import Icon from './Icon';
import Header from './Header';
import Colors from '../colors';
const NoMatch = ({ children, title, icon, kind }) => {
return ([
<div style={[styles.base, kind && styles[kind]]}>
<Icon name={icon} style={[styles.icon]} height="48" width="48" />
<Header title={title} style={[styles.header]} />
<p style={[styles.message]}>
{children}
</p>
</div>,
<div id="bizible.reportUser" style="display:none" data-email="some@email.com"/>
]
);
};
但是它返回一条错误消息,并且不会编译:
error TS2322: Type '({children, title, icon, kind}: { children: any; title: any; icon: any; kind: any; }) => Element[]' is not assignable to type 'StatelessComponent<Props>'.
Type 'Element[]' is not assignable to type 'ReactElement<any>'.
Property 'type' is missing in type 'Element[]'.
答案 0 :(得分:2)
您始终可以使用<React.Fragment>
(或<>
)包装返回值。这将解决您的问题,而无需在DOM中添加任何内容。