错误 “元素类型无效” React 组件

huangapple go评论62阅读模式
英文:

Error "Element type is invalid" React component

问题

以下是要翻译的代码部分:

import React from 'react';
import { SvgFromUri } from 'react-native-svg';
import { Image } from 'react-native';
import { AppColors } from '../constants';

const imgSize = 55;

const CompanyIcon = ({ url }) => {

    return (
        <>
            {
                url && !url.includes('favicon')
                    ? <SvgFromUri uri={url} width={imgSize} height={imgSize} onError={console.error} />
                    : <Image source={require('../assets/app_icon48.png')} style={
                        {
                            borderWidth: 1, 
                            borderColor: AppColors.LIGHT_BLUE, 
                            borderRadius: 4,
                            width: imgSize,
                            height: imgSize
                        }
                    } />
            }
        </>
    );
}

export default CompanyIcon;

希望这对你有所帮助。

英文:

Many times I've seen this error, and honestly I never know how to fix it, or how i fixed it in the past ...

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it&#39;s defined in, or you might have mixed up default and named imports.

Here's my component implementation, I'm trying to understand what's wrong.

import React from &#39;react&#39;;
import { SvgFromUri } from &#39;react-native-svg&#39;;
import { Image } from &#39;react-native&#39;;
import { AppColors } from &#39;../constants&#39;;

const imgSize = 55;

const CompanyIcon = ({ url }) =&gt; {

    return (
        &lt;&gt;
            {
                url &amp;&amp; !url.includes(&#39;favicon&#39;)
                    ? &lt;SvgFromUri uri={url} width={imgSize} height={imgSize} onError={console.error} /&gt;
                    : &lt;Image source={require(&#39;../assets/app_icon48.png&#39;)} style={
                        {
                            borderWidth: 1, 
                            borderColor: AppColors.LIGHT_BLUE, 
                            borderRadius: 4,
                            width: imgSize,
                            height: imgSize
                        }
                    } /&gt;
            }
        &lt;/&gt;
    );
}

export default CompanyIcon;

Thanks so much for any help community

The options I found research about it said something about exporting the component as default, so I did, but no success 错误 “元素类型无效” React 组件


Update:
Looks like it's a bug within the svg component 错误 “元素类型无效” React 组件
https://github.com/software-mansion/react-native-svg/issues/1742

答案1

得分: 1

检查你的导出,如果你有一个默认导出,你应该像这样导入:

import FunctionName from '../location'; // 不用 {}

或者你可能忘记从定义组件的文件中导出它,或者你可能弄混了默认导入和命名导入。

英文:

check your exports, if you have a default export you should import like this :

import FunctionName from &#39;../location&#39;; // without {}

or maybe you
>forgot to export your component from the file it's defined in, or you might have mixed up default and named imports

huangapple
  • 本文由 发表于 2023年2月6日 10:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定