React-native:Undefined不是对象(评估'this.props.navigation')

时间:2018-04-10 15:51:52

标签: react-native navigation stack-navigator

很乐意使用你的帮助。 我在反应原生中乱搞堆栈导航器,但是我已经在标题中提到的错误上暂时停留了一段时间。我使代码成为最基本的代码,但仍然无法弄清楚bug。我想这是关于this.props的可访问性的东西,但我还不明白..

下面是代码:

App.js

import React, {Component} from 'react';
import {AppRegistry,Animated,DeviceEventEmitter,Text,View,StyleSheet,TouchableHighlight,TouchableOpacity,TextInput,Button,Image,Dimensions} from 'react-native';
import {StackNavigator} from '/Users/amitnelinger/Desktop/MobileProjects/newProj/node_modules/react-navigation'




const App = (props) => {
    return(
     const { navigate } = props.navigation;
     <View>
                <Button 
                    onPress={() => navigate('song')} 
                    title = "MotherFucker Jones"
                />  

      </View>
    );
 }

App.navigationOptions = {
    title: 'Home screen',
};

export default App

index.ios.js

import React, {Component} from 'react';
import {AppRegistry,Animated,DeviceEventEmitter,Text,View,StyleSheet,TouchableHighlight,TouchableOpacity,TextInput,Button,Image,Dimensions} from 'react-native';

import App from '/Users/amitnelinger/Desktop/MobileProjects/newProj/app/components/App.js'
import song from '/Users/amitnelinger/Desktop/MobileProjects/newProj/app/components/song.js'
import {StackNavigator} from '/Users/amitnelinger/Desktop/MobileProjects/newProj/node_modules/react-navigation'

export default class newProj extends Component{

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

        return(
            <App navigation = { navigation } />
        );
    }
}

const simpleApp = StackNavigator({
    Home: {screen: App},
    song: {screen: song}
});

AppRegistry.registerComponent('newProj',() => newProj)

2 个答案:

答案 0 :(得分:0)

在我看来,你并没有正确地调用导航。改变导航的完整道具是'this.props.navigation.navigate('song')。试试这个调整

答案 1 :(得分:0)

你没有使用StackNavigator,你只是传递一个未定义的导航道具。使用StackNavigator交换应用程序,它应该可以解决您的问题:

export default class newProj extends Component{

    render(){    
        return(
            <simpleApp />
        );
    }
}

const simpleApp = StackNavigator({
    Home: {screen: App},
    song: {screen: song}
});