javascript - 在 react-native 中创建 stackNavigator 后导航到页面时出错
问题描述
我正在使用 react-native 创建一个应用程序,我想使用createStackNavigator
三个屏幕。我有一个按钮,我想用它来将用户带到第三个屏幕。
这是我的代码createStackNavigator
const RootStack = createStackNavigator({
Login: {
screen: LoginActivity
},
Profile: {
screen: ProfileActivity
},
MakeAccount: {
screen: MainProject
}
});
const AppContainer = createAppContainer(RootStack);
export default AppContainer;
这是我导航到页面/屏幕的按钮的代码
<Button title="Click Here To Create an Account" onPress={() => this.props.navigation.navigate('MakeAccount')} color="#2196F3" />
当我使用 Xcode 在 iso 模拟器上运行我的应用程序时,我收到以下消息:
我不知道为什么会这样……
另外,这是我的MainProject
课:
class MainProject extends Component {
constructor(props) {
super(props)
this.state = {
UserName: '',
UserEmail: '',
UserPassword: ''
}
}
UserRegistrationFunction = () => {
const { UserName } = this.state;
const { UserEmail } = this.state;
const { UserPassword } = this.state;
fetch('http://ip/user_registration.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: UserEmail,
password: UserPassword
})
}).then((response) => response.json())
.then((responseJson) => {
Alert.alert(responseJson);
}).catch((error) => {
console.error(error);
});
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={{ fontSize: 20, color: "#000", textAlign: 'center', marginBottom: 15}}>User Registron Form</Text>
<TextInput
placeholder="Enter User Name"
onChangeText={UserName => this.setState({UserName})}
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
/>
<TextInput
placeholder="Enter User Email"
onChangeText={UserEmail => this.setState({UserEmail})}
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
/>
<TextInput
placeholder="Enter User Password"
onChangeText={UserPassword => this.setState({UserPassword})}
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
secureTextEntry={true}
/>
<Button title="Click Here To Register" onPress={this.UserRegistrationFunction} color="#2196F3" />
</View>
);
}
}
export default MainProject;
解决方案
看你贴在这里的代码,好像创建stackNavigator
就ok了。错误信息非常清楚地说明了问题:
“MakeAccount”必须是一个 React 组件。
因此,这可能意味着您没有React.Component
在MainProject
类上进行扩展,或者您的导入/导出在MainProject
实例化或导入类的位置不正确(这应该在您创建堆栈的同一文件中)。
在您实例化的文件上,它必须是这样的MainProject
:
import React from "react";
export default class MainProject extends React.Component {
...
}
然后将其导入您正在创建导航器的位置:
import MainProject from "./CompletePathToMainProject";
...
//Your logic of stackNavigator creation
我不知道你的文件夹和文件的结构,所以上面的CompletePathToMainProject
意思是./account/MainProject.js
基于你到目前为止提供的信息
推荐阅读
- wordpress - 如何将网站 301 永久重定向到另一个网站和永久链接的子类别(使用 .htaccess)
- r - 如何在R中遍历数据框列中的行值
- java - 解析问题-XML java
- node.js - 错误 [ERR_HTTP_HEADERS_SENT]:在以快递方式发送到客户端后无法设置标头
- typescript - How to use a switch statement to narrow down a type using conditional types?
- c# - 如何从 amazon s3 模拟/单元测试下载?
- django - 如果为 True,则显示布尔字段结果
- azure-ad-b2c - 如何为自定义策略启用 PKCE 配置
- html - 媒体查询在 Firefox 中不起作用,但在 Chrome 中起作用
- mysql - 当等于 SQL 中的某些内容时,如何将列过滤到行的不同部分?