7

我正在尝试将此简单添加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[]'.
4

1 回答 1

5

您总是可以用<React.Fragment>(或<>)包装返回值。这将在不向 DOM 添加任何内容的情况下解决您的问题。

于 2020-07-16T12:07:22.347 回答