为什么错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:数字

2024-03-22

我正在运行这个程序来显示 SVG 图像

import React, { Component } from 'react';
import { View } from 'react-native';
import Expo from './assets/check-mark.svg';
import SVG from 'react-native-svg';

export default class MyApp extends Component {
 render(){
  return (
    <View style={{backgroundColor:"black"}}>
    <Expo width={20}
    height={15} />
    </View>
   );
 }
}

并遇到了这个错误Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: number.

我该如何解决这个问题? 或者还有其他更好的方法来使用 React Native 的 SVG 图像吗?


对于 svg 文件,react-native-svgsuggests https://github.com/react-native-svg/react-native-svg#use-with-svg-files的用法反应本机 svg-变压器 https://github.com/kristerkari/react-native-svg-transformer.

安装包:

yarn add --dev react-native-svg-transformer

将配置添加到metro.config.js:

const { getDefaultConfig } = require('metro-config');

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts },
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve('react-native-svg-transformer'),
    },
    resolver: {
      assetExts: assetExts.filter(ext => ext !== 'svg'),
      sourceExts: [...sourceExts, 'svg'],
    },
  };
})();

这样你就应该能够渲染你的 SVG 文件了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:数字 的相关文章

随机推荐