英文:
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'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 '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;
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
Update:
Looks like it's a bug within the svg component
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 '../location'; // 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论