2017-07-31 67 views
0

我试图让我的第一阵营原生Android应用程序和我收到此错误:不确定是不是(评估“this.props.navigation.navigate”)的对象 - 阵营本地

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

这是代码:

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

export default class HomeScreen extends React.Component { 

    static navigationOptions = { 
    title: 'Home', 
    }; 

    render() { 
    const { navigate } = this.props.navigation;  

    return (
     <View> 
     <Button 
      title="Show Centers near me" 
      onPress={() => 
      navigate('Results', "Search Term") 
      } 
      /> 
     <Text>or</Text> 
     </View> 
    ); 
    } 
} 


class ResultsScreen extends React.Component { 

    static navigationOptions = { 
    title: 'Results', 
    }; 


    render() { 
    const { navigate } = this.props.navigation; 

    return (
     <View> 
     <Text>Hi</Text> 
     </View> 
    ); 
    } 
} 

const App = StackNavigator({ 
    Home: { screen: HomeScreen }, 
    Results: { screen: ResultsScreen } 
}); 

我不明白为什么错误即将到来。

+0

尝试在你的类中添加一个构造函数,像这样:'构造函数(道具){超(道具)}' –

+0

@HelderJulesDeBaere隐而不宣”帮助! – Akshey

+1

显示有关如何设置StackNavigator的更多代码。 this.props.navigation由导航器传递,如果设置关闭,则可能会发生此错误。也尝试搜索这个,这已被回答了很多。 – hyb175

回答

3

您正在导出组件错误。你应该得到你的class HomeScreen定义,并在文件的底部摆脱export defaultexport default App;

+0

谢谢。这工作。 – Akshey

相关问题