我正在开始一个新的React Native项目,我使用了'expo init'并选择了一个空白的托管项目作为模板。我有几个来自不同项目的屏幕和组件,我想复制到我的新屏幕中。我收到以下错误:
不变违反:元素类型无效:预期为字符串(对于 内置组件)或类/函数(用于复合组件) 但得到:未定义。您可能忘记了从中导出组件 定义的文件,或者您可能混淆了默认文件并命名为 进口。
检查
CreateAccountForm
的呈现方法。
我不知道发生了什么。我很确定我的所有设置都与我在第一个项目中所做的完全一样,它可以使一切正常。我正在使用React Navigation,我的新项目将“ HomeScreen”很好地渲染为“ initialRouteName”。但是,每当我尝试将初始路由设置为“ CreateNewAccountScreen”时,都会收到上述错误。
我已经对其进行了测试,并且只要它不尝试呈现嵌套在其中的“ CreateAccountForm”组件,“ CreateNewAccountScreen”就可以作为我的初始路径。将<CreateAccountForm>
组件替换为简单的<Text>Hi!<Text>
后,它与<Advertisement>
组件一起使屏幕正常显示,没有问题。
HomeScreeen:
import React from 'react';
import { StyleSheet, Image, Button, View } from 'react-native';
import Advertisement from '../components/Advertisement';
const HomeScreen = ({navigation}) => {
return (
<View style={styles.mainContainer}>
<View style={styles.logoContainer}>
<Image style={styles.logo}
source={require('../assets/TPLookupLogo.png')}
style={{height: 200, width: 350, marginBottom: 40}}
resizeMode="contain">
</Image>
</View>
<View style={styles.btnsContainer}>
<Button
style={styles.button}
appearance="outline"
onPress={()=>{console.log('To New User')}}
title='New User'
/>
<Button
style={styles.button}
appearance="outline"
onPress={()=>{console.log('To Login')}}
title='Login'
/>
</View>
<View style={styles.adContainer}>
<Advertisement/>
</View>
</View>
);
}
const styles = StyleSheet.create({
mainContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
logoContainer: {
flex: 4,
justifyContent: 'flex-end',
alignItems: 'center'
},
btnsContainer: {
flex: 4,
width: '40%',
justifyContent: 'flex-start',
},
button: {
marginVertical: 4,
},
adContainer: {
flex: 1,
justifyContent: 'center',
backgroundColor: 'black'
}
})
export default HomeScreen;
AppNavigator:
import { createStackNavigator } from 'react-navigation-stack';
import HomeScreen from '../screens/HomeScreen';
import CreateNewAccountScreen from '../screens/CreateNewAccountScreen';
const AppNavigator = createStackNavigator(
{
Home: HomeScreen,
CreateNewAccount: CreateNewAccountScreen
},
{
initialRouteName: 'CreateNewAccount'
}
)
export default AppNavigator;
CreateNewAccountScreen:
import React from 'react';
import { StyleSheet, View } from 'react-native'
import CreateAccountForm from '../components/CreateAccountForm';
import Advertisement from '../components/Advertisement';
const CreateNewAccountScreen = () => {
return (
<View style={styles.mainContainer}>
<View style={styles.formContainer}>
<CreateAccountForm/>
</View>
<View style={styles.adContainer}>
<Advertisement/>
</View>
</View>
);
}
const styles = StyleSheet.create({
mainContainer:{
flex: 1,
},
formContainer: {
flex: 8,
},
adContainer: {
flex: 1,
justifyContent: 'center',
backgroundColor: 'black'
}
})
CreateNewAccountScreen.navigationOptions = {
headerTitle: 'Create Account'
}
export default CreateNewAccountScreen;
CreateAccountForm:
import React, { useState } from 'react';
import { StyleSheet, View, Input, Button } from 'react-native';
const CreateAccountForm = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [company, setCompany] = useState('');
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [address, setAddress] = useState('');
const [city, setCity] = useState('');
const [stateName, setStateName] = useState('');
const [zip, setZip] = useState('');
const onChangeEmailHandler = value => {
setEmail(value);
}
const onChangePasswordHandler = value => {
setPassword(value);
}
const onChangeCompanyHandler = value => {
setCompany(value);
}
const onChangeFirstNameHandler = value => {
setFirstName(value);
}
const onChangeLastNameHandler = value => {
setLastName(value);
}
const onChangeAddressHandler = value => {
setAddress(value);
}
const onChangeCityHandler = value => {
setCity(value);
}
const onChangeStateNameHandler = value => {
setStateName(value)
}
const onChangeZipHandler = value => {
setZip(value);
}
const RegisterUserHandler = props => {
let emailLength = email.length;
let passwordLength = password.length;
if (emailLength === 0 || passwordLength === 0)
{
console.log('Email & Password cannot be blank.');
}
else
{
registerUser()
}
}
async function registerUser () {
let headers = {
'X-Authorization': "",
'Accept': 'application/json',
'Content-Type': 'application/json',
};
let body = JSON.stringify({
Email: email,
Password: password,
Company: company,
FirstName: firstName,
LastName: lastName,
Address: address,
City: city,
State: stateName,
Zipcode: zip
})
let response = await fetch('',
{
method: 'POST',
headers: headers,
body: body
});
let responseJson = await response.json()
}
return (
<View style={styles.mainContainer}>
<Input
style={styles.input}
type="text"
value={email}
placeholder="Email"
onChangeText={onChangeEmailHandler}
/>
<Input
style={styles.input}
type="text"
value={password}
placeholder="Password"
onChangeText={onChangePasswordHandler}
/>
<Input
style={styles.input}
type="text"
value={company}
placeholder="Company"
onChangeText={onChangeCompanyHandler}
/>
<Input
style={styles.input}
value={firstName}
placeholder="First Name"
onChangeText={onChangeFirstNameHandler}
/>
<Input
style={styles.input}
value={lastName}
placeholder="Last Name"
onChangeText={onChangeLastNameHandler}
/>
<Input
style={styles.input}
value={address}
placeholder="Address"
onChangeText={onChangeAddressHandler}
/>
<View style={styles.rowInputsContainer}>
<Input
style={styles.input}
value={city}
style={styles.rowInput}
placeholder="City"
onChangeText={onChangeCityHandler}
/>
<Input
style={styles.input}
value={stateName}
style={{...styles.rowInput, ...styles.centerRowInput}}
placeholder="State"
onChangeText={onChangeStateNameHandler}
/>
<Input
style={styles.input}
value={zip}
style={styles.rowInput}
placeholder="Zip"
onChangeText={onChangeZipHandler}
/>
</View>
<Button
style={styles.btn}
onPress={RegisterUserHandler}
title='Register'
/>
</View>
)
}
const styles = StyleSheet.create({
mainContainer: {
flex: 1,
width: '75%',
alignSelf: 'center',
justifyContent: 'center',
},
rowInputsContainer: {
display: 'flex',
flexDirection: 'row',
marginBottom: 16
},
rowInput: {
flexGrow: 1,
},
centerRowInput: {
marginHorizontal: 4
},
input: {
marginVertical: 8
}
})
export default CreateAccountForm;
这个完全相同的设置可以使我的第一个应用程序一切正常。所以我不明白我哪里出了问题。任何帮助,不胜感激,谢谢,和平!
答案 0 :(得分:1)
React Native具有TextInput
组件,没有Input
组件。能否在 CreateAccountForm 中导入它时进行检查。