2017-04-11 51 views
0

我是React Native的新手,嘗試(按照教程)移動到另一個屏幕時單擊按鈕,這是我更新的代碼以及按下按鈕時出現的錯誤照片。 index.ios.js:未定義不是對象(評估'this.props._navigate.replace/push')

export default class Application extends Component { 

    constructor(){ 
    super(); 
    } 

    renderScene(route, navigator){ 
    if(route.name == 'login'){ 
     return <Login navigator={navigator}/> 
    }else if(route.name == 'home'){ 
     return <Home navigator={navigator}/> 
    } 
    } 

    render() { 
    return <Navigator 
     initialRoute={{name: 'login'}} 
     renderScene={this.renderScene.bind(this)}/> 
    } 
} 

LoginForm的:

export default class LoginForm extends Component{ 
    constructor(props){ 
     super(props); 
     this.navigate = this.navigate.bind(this); 
    } 

    navigate(routeName){ 
     this.props.navigator.replace({ 
      name: routeName, 
      Component: Home 
     }); 
    } 
    render(){ 
     return(
      <View style={mStyle.container}> 
       <StatusBar barStyle='light-content'/> 
       <TouchableOpacity> 
        <Text style={mStyle.btnText}> 
         LOGIN WITH FACEBOOK 
        </Text> 
       </TouchableOpacity> 
       <TouchableOpacity onPress={()=>this.navigate('home')}> 
        <Text style={mStyle.noLoginTxt}> 
         CONTINUE WITHOUT LOGIN 
        </Text> 
       </TouchableOpacity> 
      </View> 
     ); 
    } 
} 

我想按鈕被點擊時從登錄屏幕移動到主屏幕。 按下模擬器上的按鈕時出現此問題: enter image description here

+0

您能否向我們提供一個如何使用'Navigator'的示例? –

+0

我剛開始使用React Native,所以我做了他在這裏做的:https://www.youtube.com/watch?v = Z7u5wBQB-vg –

回答

0

您遇到的一些問題。

1.-的語法是:

this.props.navigator.{any_method} 

如:

  • this.props.navigator.push
  • this.props.navigator.replace

this.props._navigate.{any_method} 

2.-如果你有孩子的意見請確保你發送給孩子的Navigator財產,如。

<View> 
    <MyChildComponent navigator={this.props.navigator} /> 
</View> 

3.-確保您的子視圖從構造函數的父級獲取道具。

變化

constructor(){ 
    super(); 
    this._navigate = this._navigate.bind(this); 
} 

constructor(props){ 
    super(props); 
    // The following line is not required since you are using an arrow function to call this one. 
    // this._navigate = this._navigate.bind(this); 
} 
+0

我試過了你的建議,但仍然遇到同樣的問題。 –

+0

你可以編輯你的問題,通過添加視圖來呈現你對問題的看法,所以我們可以看到更詳細的問題。謝謝。 –

+0

完成。我從這段視頻中瞭解到:https://www.youtube.com/watch?v=Z7u5wBQB-vg我剛開始學習React Native。 –

0

那是因爲有你在Navigator,並在組件

改變使用性質之間的不匹配

_navigate(name){ 
     this.props._navigate.replace({ //I also tried push, same problem. 
      name 
     }); 
} 

_navigate(name){ 
     this.props.navigator.replace({ //I also tried push, same problem. 
      name 
     }); 
} 
+0

我累了,現在是這樣,但仍然是同樣的問題。 –

+0

你可以用上面的代碼發佈錯誤。除非你在導航器中做了不同的事情,否則這應該起作用。 –

+0

另外請確保你做props.navigator而不是props.navigate –

相關問題