无法解析本地文件的模块(React Native)

我奇怪地得到一个错误,即React Native无法解析本地文件导入 .

Unable to resolve module `./components/MyComponent" from ".//App.js`: could not resolve `/Users/myusername/Desktop/mylibrary/components/MyComponent' as a file nor as a folder","name":"UnableToResolveError","type":"UnableToResolveError","errors":[{}]},"type":"bundling_error"}"

在MyLibrary / App.js:

import React from 'react';
import {
  View,
  Text,
  StyleSheet,
  ScrollView,
  AsyncStorage,
} from 'react-native';

import MyComponent from './components/MyComponent';

在MyLibrary /组件/ MyComponent.jsx:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

export default class MyComponent extends React.Component {

    render() {
        <View>
            {this.props.children}
        </View>
    }
}

我尝试将文件重命名为小写( mycomponent.jsx ),但它没有帮助 .

回答(2)

2 years ago

该组件是错误的 . 您没有在render方法中返回任何内容 . 试试这个:

render() {

return(
        <View>
            {this.props.children}
        </View>
)
    }

2 years ago

默认情况下, import 语句尝试导入 .js 文件,尝试将 MyComponent.jsx 重命名为 MyComponent.js .

引用MDN

要从中导入的模块 . 这通常是包含模块的.js文件的相对或绝对路径名,不包括.js扩展名 . 某些捆绑商可能允许或要求使用扩展;检查你的环境 . 只允许使用单引号和双引号字符串 .