2017-06-12 39 views
0

我是React native的新手,尝试跟随教程学习,但无法获得屏幕之间的导航工作。我以前使用一个屏幕就可以正常工作,但是当添加导航时,我会收到错误。React本地导航 - 未定义不是对象

在此之后:https://facebook.github.io/react-native/releases/next/docs/navigation.html

给了我这样的代码:

import React from 'react'; 
import { 
    StackNavigator, 
} from 'react-navigation'; 

export default class HomeScreen extends React.Component { 
    static navigationOptions = { 
    title: 'Welcome', 
    }; 
    render() { 
    const { navigate } = this.props.navigation; 
    return (
     <Button 
     title="Go to Jane's profile" 
     onPress={() => 
      navigate('Profile', { name: 'Jane' }) 
     } 
     /> 
    ); 
    } 
} 

class ProfileScreen extends React.Component { 
    static navigationOptions = { 
    title: 'Profile', 
    }; 
    render() { 
    return (
     <Button title="do nothing"/> 
    ); 
    } 
} 

const App = StackNavigator({ 
    Home: { screen: HomeScreen }, 
    Profile: { screen: ProfileScreen }, 
}); 

试图在错误

未定义运行这个(通过世博会的应用程序)的结果不是一个对象(评估'this.props.navigation.navigate')

所以我正在做导航,我该如何解决这个错误?

+0

如果是U渲染'' Ricky

+0

你能展现您调用的index.ios.js文件 'AppRegistry.registerComponent('MyAwesomeApp',()=> App);' – Dpkstr

+0

您的HomeScreen组件中是否有构造函数?也许你无法访问道具。阅读:https://facebook.github.io/react/docs/react-component.html#constructor –

回答

0

您需要在App中使用AppRegistry.registerComponent。 这样做的好方法是创建一个src目录,然后在那里创建2个js文件,HomeScreen和ProfileScreen。然后在与index.android.js或index.ios.js相同的级别创建和App.js,并包含这两个文件,并像您在代码中一样声明StackNavigator,而不是const App您需要拥有const NameOfYourApp,其中NameOfYourApp是您在运行create-react-native-app时为您的项目命名的方式(如果它是App,就这样放置它) 然后,您需要在最后添加此行AppRegistry。 registerComponent('NameOfYourApp',()=> NameOfYourApp);

import React from 'react'; 
import { 
    StackNavigator, 
} from 'react-navigation'; 
import HomeScreen from './src/HomeScreen' 
import ProfileScreen from './src/ProfileScreen' 

const NameOfYourApp = StackNavigator({ 
    Home: { screen: HomeScreen }, 
    Profile: { screen: ProfileScreen }, 
}); 
AppRegistry.registerComponent('NameOfYourApp',() => NameOfYourApp) 

的最后一步是导入App.js文件在您index.android.js或index.ios.js

import './App'; 
相关问题