我正在尝试将这个简单的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[]'.
您始终可以使用<React.Fragment>
(或<>
(包装返回值。这将解决您的问题,而无需向 DOM 添加任何内容。