【问题标题】:undefined is not an object (evaluating _this2.props.navigation.navigate)undefined 不是对象(评估 _this2.props.navigation.navigate)
【发布时间】:2017-09-13 16:46:05
【问题描述】:

我正在使用 react-native,但我遇到了导航器的问题。

代码:

App.js

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

import { StackNavigator } from 'react-navigation';

import loginScreen from './src/components/loginView';
import registerScreen from './src/components/registerView';

const Appmemes = StackNavigator({
   Login: {screen: loginScreen},
   Register: {screen: registerScreen}
});

export default Appmemes;

AppRegistry.registerComponent('Appmemes', () => Appmemes);

loginView.js 工作正常:

.
.
.
  <View style={styles.formContainer}>
          <LoginForm/>
  </View>
.
.
.    

LoginForm.js

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

export default class LoginForm extends Component{
  render() {
      return(
      <View style={styles.container}>

          <TouchableOpacity style={styles.buttonContainer1}>
            <Text style={styles.buttonText}>Entrar</Text>
          </TouchableOpacity>

          <TouchableOpacity style={styles.buttonContainer2} onPress={() => this.props.navigation.navigate('Login')}>

            <Text style={styles.buttonText}>Registrarse</Text>
          </TouchableOpacity>
      </View>
    );
  }
}

AppRegistry.registerComponent('LoginForm', () => LoginForm);

const styles = StyleSheet.create({...]);
});

错误是:

undefined 不是对象(评估 _this2.props.navigation.navigate)

错误在 LoginForm.js 中的OnPress()

onPress={() => this.props.navigation.navigate('Login')

此错误的原因可能是什么?

【问题讨论】:

  • Este es el sitio en inglés, formula la pregunta en inglés o puedes pasarte al sitio español
  • 我们应该如何理解这一点?
  • 不明白,但我打赌你正在寻找THIS
  • 感谢您的回答! ...Gerardo Muchas gracias por la aclaración, lamento mi equivocación, saludos
  • 不注册登录界面,默认导出

标签: javascript node.js reactjs react-native react-native-navigation


【解决方案1】:

很简单。 &lt;LoginForm navigation={this.props.navigation} /&gt;

发生错误是因为 LoginFrom 没有直接使用 StackNavigator 加载,只有那些由 StackNavigator 直接加载的组件才能获得 navigation 道具(如您的情况下的 loginScreen)。 loginScreen 内的任何嵌套组件都不会自动接收navigation 属性,因此我们需要将其显式传递给LoginForm,因为它将被传递给loginScreen.

我已经回答了一个类似的问题here

旁注:我相信您再次使用this.props.navigation.navigate('Login')loginScreen 内部导航到loginScreen,因为LoginFormloginScreen 内部。所以在这种情况下,您可能是要导航到Register。所以你应该写this.props.navigation.navigate('Register')

另外,你也不需要AppRegistry.registerComponent('LoginForm', () =&gt; LoginForm); 这是因为你只需要用AppRegistry注册一个根组件。所以,Appmemes 应该只在你已经在做的AppRegistry 注册。 LoginForm 会自动被loginScreen 挂载,而Appmemes 又会被Appmemes 挂载。

【讨论】:

  • Mustan bhai .. 我如何在下面的代码中使用您的逻辑:&lt;TouchableOpacity onPress = {() =&gt; this.props.navigation.navigate('NewPage',{id:'5'})}&gt; &lt;FlexCardComp mainId='5' val1='501' val2='502' /&gt; &lt;/TouchableOpacity&gt;
  • navigation 属性传递给您在屏幕中使用的所有需要​​执行this.props.navigation 的组件。就像我在您的案例中为 LoginForm 组件展示的那样。
  • 那会是&lt;LoginForm navigation={this.props.navigation.navigate('newPage,{id:'5'}')} /&gt; 吗?
  • 这样试试。 &lt;LoginForm navigation={this.props.navigation} /&gt;
  • 这对我有用,只是继续传递那些导航道具。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多