gpt4 book ai didi

react-native - react 原生 : TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate' )

转载 作者:行者123 更新时间:2023-12-04 23:38:08 24 4
gpt4 key购买 nike

作为react-native的初学者,我无法找出代码中的问题。通过在互联网上阅读,我觉得我可能遇到了一些绑定(bind)问题。

所以,我的代码从 index.js 开始,并在那里注册 App 组件。 app 组件只包含堆栈导航路线。它加载 LoginScreen 组件(显示应用程序的 Logo 、背景和名称),后者又加载 LoginForm 组件。登录按钮上没有身份验证,我唯一需要的是在按下登录按钮时加载菜单组件。它给出了 TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate')

索引.js

import { AppRegistry } from 'react-native';
import App from './App';

AppRegistry.registerComponent('bluebulk', () => App);

App.js

import { StackNavigator } from 'react-navigation';
import LoginScreen from './src/components/login/LoginScreen';
import Menu from './src/components/menu/Menu';

const App = StackNavigator({
Main: { screen: LoginScreen },
Menu: { screen: Menu }
});

export default App;

登录界面.js

import { StackNavigator } from 'react-navigation';
import React, { Component } from 'react';
import { StyleSheet, View, Text, Image } from 'react-native';
import LoginForm from './LoginForm';

class LoginScreen extends Component {

render() {
return (

<View style={styles.container}>
<View style={styles.logoContainer}>
<Image
style={styles.logo}
source={require('../../images/transparent.png')}
/>
<View style={{ flexDirection: 'row' }}>
<Text style={styles.blueTextStyle}>Blue</Text>
<Text style={styles.bulkTextStyle}>Bulk</Text>
</View>
</View>
<View style={styles.formContainer}>
<LoginForm />
</View>
</View>


);
}
}


export default LoginScreen;

登录表单.js

import React, { Component } from 'react';
import {
StyleSheet,
TextInput,
TouchableOpacity,
Text,
View,
KeyboardAvoidingView,
Keyboard
} from 'react-native';
import { StackNavigator } from 'react-navigation';

class LoginForm extends Component {

render() {
return (

<KeyboardAvoidingView behavior='height' style={styles.container}>

<View style={{ flexDirection: 'row' }}>
<Text style={styles.textStyle}>Email:</Text>
<TextInput
style={styles.styleInput}
placeholder="user@gmail.com"
returnKeyType="next"
keyboardType="email-address"
onSubmitEditing={() => this.refs.password.focus()}
/>
</View>

<View style={{ flexDirection: 'row' }}>
<Text style={styles.textStyle}>Password:</Text>
<TextInput
ref='password'
style={styles.styleInput}
placeholder="password"
secureTextEntry
returnKeyType="go"
onSubmitEditing={Keyboard.dismiss}
/>
</View>

<TouchableOpacity
style={styles.buttonContainer}
onPress={() => this.props.navigation.navigate('Menu')} //Error here
>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
</KeyboardAvoidingView>

);
}
}

export default LoginForm;

菜单.js

import React, { Component } from 'react';
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native';
import { StackNavigator } from 'react-navigation';

class Menu extends Component {

render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<View style={styles.viewContainer}>

<TouchableOpacity style={styles.buttonContainer}>
<Text style={styles.buttonText}>View Products</Text>
</TouchableOpacity>

<TouchableOpacity style={styles.buttonContainer}>
<Text style={styles.buttonText}>View Discounts/Offers</Text>
</TouchableOpacity>

<TouchableOpacity style={styles.buttonContainer}>
<Text style={styles.buttonText}>View Invoice History</Text>
</TouchableOpacity>

</View>

</View>
);
}
}



export default Menu;

最佳答案

您需要将导航 Prop 向下传递给您的 LoginForm 组件。

试试这个:<LoginForm navigation={this.props.navigation} />

你应该得到以下结果:

enter image description here

关于react-native - react 原生 : TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47478570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com