React.createElement: 使用多个 Helmet React Typescript 时类型无效

React.createElement: type is invalid when using multiple Helmet React Typescript

我有一个 seo 组件给我 React.createElement 错误,从过去 2 天开始我一直在努力让它工作,有人可以帮我解决这个问题

SeoModule.tsx

import * as React from 'react';
import Helmet from 'react-helmet';
import FacebookSeo from './FacebookSeo';
import TwitterSeo from './TwitterSeo';

type propTypes = {
    ...
}

export default class SeoModule extends React.Component<propTypes> {
    render() {
        return (
            <> 
                <Helmet title = {seo.title}>
                    <script type="application/ld+json">{
                        JSON.stringify(structuredData)
                    }</script>
                </Helmet>    
                <FacebookSeo
                    desc={seo.desc}
                    image={seo.image}
                    title={seo.title}
                    type={event ? 'event' : null}
                    url={seo.url}
                />
                <TwitterSeo
                    type={event ? 'summary_large_image' : null}
                    title={seo.title}
                    image={seo.image}
                    desc={seo.desc}
                    username={twitter}
                />  
            </>
        )
    }
}

和FacebookSeo.tsx

import * as React from 'react';
import Helmet from 'react-helmet';

type propTypes = {
  ...
};

export default class FacebookSeo extends React.Component<propTypes> {
  render() {
    const {url, type, title, desc, image} = this.props;
    return (
      <Helmet>
        {url && <meta property="og:url" content={url} />}
        {type && <meta property="og:type" content={type} />}
        {title && <meta property="og:title" content={title} />}
        {desc && <meta property="og:description" content={desc} />}
        {image && <meta property="og:image" content={image} />}
      </Helmet>
    );
  }
}  

和TwitterSeo.tsx

import * as React from 'react';
import Helmet from 'react-helmet';

type propTypes = {
  ...
};

export default class TwitterSeo extends React.Component<propTypes> {
  render() {
    const {username, type, title, desc, image} = this.props;
    return (
      <Helmet>
        <meta name="twitter:card" content={type} />
        {username && <meta name="twitter:creator" content={username} />}
        {title && <meta name="twitter:title" content={title} />}
        {desc && <meta name="twitter:description" content={desc} />}
        {image && <meta name="twitter:image" content={image} />}
      </Helmet>
    );
  }
}

但是上面的代码报错

React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) 但得到:未定义。您可能忘记从其定义的文件中导出您的组件。

检查 SeoModule 的渲染方法。

6 | export default (props) =>

答案:

 export default class FacebookSeo extends React.Component<propTypes> {
  render() {
    const {url, type, title, desc, image} = this.props;
    return (
    <div>
      <Helmet>
        {url && <meta property="og:url" content={url} />}
        {type && <meta property="og:type" content={type} />}
        {title && <meta property="og:title" content={title} />}
        {desc && <meta property="og:description" content={desc} />}
        {image && <meta property="og:image" content={image} />}
      </Helmet>
    </div>
    );
  }
}  

将呈现的代码包裹在 div 标签中,因为头盔代码需要嵌入到 div 或 HTML 元素中