2017-08-11 47 views
0

错误:“未定义是不是一个对象(计算‘这个,props.navigation.navigate’)”在本地做出反应

收到错误导航一个页面时,其他页面阵营本土。

错误:

undefined is not an object(evaluating 'this,props.navigation.navigate')

代码:

import { StackNavigator,NavigationActions } from "react-navigation"; 
const Navigation = StackNavigator({ 
    Home : { 
    screen : Home 
    }, 
}) 
export default class App extends React.Component { 
    submit =() => { 
    this.props.navigation.navigate('Home'); 

} 
    render() { 
    return (
     <View style={styles.container}> 
     <Text>Enter Log</Text> 
     <TextInput style={styles.input} 
      multiline={true} 
      underlineColorAndroid="transparent" 
      placeholder="Enter Log" 
      placeholderTextColor="#9a73ef" 
      autoCapitalize="none" 
      onChangeText={this.handlePassword} /> 
     <TouchableOpacity style={styles.submitButton} onPress={() => submit } >  
        <Text style={styles.submitButtonText}> Submit </Text> 
     </TouchableOpacity> 
     </View> 
    ); 
    } 
} 
} 

回答

0
  1. 试着改变你的onPress值这样:

    onPress={this.submit}  
    
  2. 阿尔斯o,我没有看到你将你的Home组件导入到你指定为屏幕的位置。

+0

变化后给出了错误:找不到变量:提交 使用链接:HTTPS://facebook.github。 io/react-native/docs/tutorial.html –

+0

除了我提出的建议之外,你还做过其他更改吗? 尝试只留下“{submit}”,而不要“this”。并且没有“()=>”。这背后的原因是你不需要调用提交函数,只需传递它。 – Eduard

0

你必须绑定你的方法:

import { StackNavigator,NavigationActions } from "react-navigation"; 
const Navigation = StackNavigator({ 
    Home : { 
    screen : Home 
    }, 
}) 
export default class App extends React.Component { 

    constructor(props) { 
     super(props); 

     this.submit = this.submit.bind(this); 
    } 


    render() { 
    return (
     <View style={styles.container}> 
     <Text>Enter Log</Text> 
     <TextInput style={styles.input} 
      multiline={true} 
      underlineColorAndroid="transparent" 
      placeholder="Enter Log" 
      placeholderTextColor="#9a73ef" 
      autoCapitalize="none" 
      onChangeText={this.handlePassword} /> 
     <TouchableOpacity style={styles.submitButton} onPress={this.submit} >  
        <Text style={styles.submitButtonText}> Submit </Text> 
     </TouchableOpacity> 
     </View> 
    ); 
    } 

    submit() { 
     this.props.navigation.navigate('Home'); 
    } 

} 

说明:Binding context when calling ES6 method. How to access object from within method called as callback?

+0

请建议任何链接 –

相关问题