2017-06-12 38 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'; 
相關問題