在我的 index.ios.js 中,我有以下内容:
renderScene(route, navigator){
return <route.component navigator={navigator} {...route.passProps}/>
}
和
render() {
return (
<Navigator
initialRoute={{name: 'FirstPage', component: FirstPage}}
renderScene={this.renderScene}
/>
);
}
然后在我的 FirstPage.js 中导航到 SecondPage.js:
_navigate(){
this.props.navigator.replace({
component: SecondPage,
name: 'SecondPage'
})
}
然后在我的 SecondPage.js 中,它只是在 .在我的 ThirdPage.js 中:
_rowPressed(){
this.props.navigator.push({
component: FourthPage,
name: 'FourthPage',
})
}
<TouchableHighlight
onPress={() => this._rowPressed()}
>
<View>
<Text>Go to FourthPage</Text>
</View>
</TouchableHighlight>
然后在我的 FourthPage.js 中,我只需要:
<View style={styles.container}>
This is FourthPage.js
</View>
我从 ThirdPage.js 收到错误,它说:
Cannot read property 'push' of undefined in _rowPressed()
我似乎无法弄清楚为什么,因为 this.props.navigator.push 对于 .replace 和 .push 都工作正常,但现在我收到了这个错误。任何指导或见解将不胜感激。提前谢谢你。
最佳答案
<TouchableHighlight
onPress={this._rowPressed.bind(this)}//add this
>
<View>
<Text>Go to FourthPage</Text>
</View>
</TouchableHighlight>
你可能只需要绑定(bind)组件
关于javascript - 为什么我得到 'Cannot read property ' push' of undefined' (React Native)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38379656/