我正在关注 this medium article 在我的 react-native 项目中使用
FloatingTitleTextInputField
下面是我的项目结构
这是我的代码
HomeScreen.js
import React, {Component} from 'react';
import {Text, View, TextInput, StyleSheet} from 'react-native';
import FloatingTitleTextInputField from './customComponents/floating_title_text_input_field';
export default class HomeScreen extends Component {
render() {
return (
// <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
// <Text>My First React App</Text>
// <TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}} />
// </View>
<View style={styles.container}>
<View style={styles.container}>
<Text style={styles.headerText}>Its Amazing</Text>
<FloatingTitleTextInputField
attrName="firstName"
title="First Name"
value={this.state.firstName}
updateMasterState={this._updateMasterState}
/>
<FloatingTitleTextInputField
attrName="lastName"
title="Last Name"
value={this.state.lastName}
updateMasterState={this._updateMasterState}
/>
</View>
</View>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 65,
backgroundColor: 'white',
},
labelInput: {
color: '#673AB7',
},
formInput: {
borderBottomWidth: 1.5,
marginLeft: 20,
borderColor: '#333',
},
input: {
borderWidth: 0,
},
});
当我尝试在里面使用
FloatingTitleTextInputField
HomeScreen.js
我得到低于错误
error Unable to resolve module `./floating_title_text_input_field` from `React Native/AwesomeProject/screens/
HomeScreen.js`: The module `./floating_title_text_input_field` could not be found from `/React Native/AwesomeProject/screens/HomeScreen.js`. Indeed, none of these files exist:
* `/React Native/AwesomeProject/screens/floating_title_text_input_field(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)`
* `/React Native/AwesomeProject/screens/floating_title_text_input_field/index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)`. Run CLI with --verbose flag for more details.
Error: Unable to resolve module `./floating_title_text_input_field` from `React Native/AwesomeProject/screens/HomeScreen.js`: The module `./floating_title_text_input_field` could not be found from `/React Native/AwesomeProject/screens/HomeScreen.js`. Indeed, none of these files exist:
谁能帮我解决这个问题
如果需要更多信息,请告诉我。提前致谢。您的努力将不胜感激。
您可以清理捆绑器上的缓存:
npm start --clean-cache
您从
HomeScreen
目录中的 screens
组件引用它。因为你使用的是本地的./路径,所以它试图在screens/customComponents
中找到它。使用../customComponents/floating_title_text_input_field
应该修复它。
即使您的错误是在
require
语句中使用了错误的路径,我认为分享我如何解决这个问题可能会有用,文件导入路径不是错误的原因。我在添加一些图像资产并在我的组件中需要它们后遇到了这个问题。但是我忘了再次构建应用程序。经过几次尝试,这是对我有用的解决方案。
yarn android/ios
.yarn start
启动开发服务器。FloatingTitleTextInputField 组件似乎是一个命名导出。所以像
import { FloatingTitleTextInputField } from 'the source'
一样导入它。
或者简单地默认导出FloatingTitleTextInputField像
export default class FloatingTitleTextInputField
在floating_title_text_input_field.js文件中。
我参加聚会可能有点晚了,但是如果有人仍在寻找这个问题并且没有找到解决方案并且正在使用 Expo,请确保您从中得到错误的组件是否具有
import { StatusBar } from 'expo-status-bar';
return (
<View>
...Some Code
<StatusBar style="auto" />
<View>
)
在 return 语句中出于某种原因从 return 块中删除此导入将破坏您的代码!
对我来说,这是由于自动导入的大写字母错误。
从'./screens/HomeScreen'导入{HomeScreen};
从'./screens/homeScreen'导入{HomeScreen};