在我的 react native 应用程序中,我有一个带有后退按钮的标题组件。它有效,除了当我单击后退按钮时,它给我这个错误:undefined is not an object (evaluating this.props.navigation)
所以我按照文档 here 进行操作并尝试使用withNavigation
。但我仍然遇到同样的错误。我做错了什么?
import React from 'react'
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'
import { withNavigation } from 'react-navigation';
const HeaderWithBackBtn = (props) => {
return (
<View style={styles.container}>
{/* Back button */}
<TouchableOpacity
style={{width: 100}}
onPress={() => this.props.navigation.goBack()}>
<Text style={styles.backBtn}>Back</Text>
</TouchableOpacity>
<Text style={styles.text}> {props.screen} </Text>
{/* Placeholder button */}
<Text style={{width: 100}}></Text>
</View>
)
}
const styles = StyleSheet.create({
...
});
export default withNavigation(HeaderWithBackBtn);
最佳答案
您要包装的组件是一个功能组件,因此您需要以 props.foo
的形式而不是 this.props.foo
的形式访问您的 props。
onPress={() => props.navigation.goBack()}>
应该可以正常工作。
关于reactjs - react 导航 withNavigation 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54759344/