错误:“undefined不是对象(评估'this,props.navigation.navigate')”在React Native中

时间:2017-08-11 13:43:07

标签: react-native

在React Native中将一个页面导航到其他页面时出错。

错误:

  

undefined不是一个对象(评估'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>
    );
  }
}
}

2 个答案:

答案 0 :(得分:1)

你必须绑定你的方法:

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?

答案 1 :(得分:0)

  1. 尝试将 onPress值更改为:

    onPress={this.submit}      
    
  2. 另外,我没有看到您将Home组件导入到您将其指定为屏幕的位置。