react-navigation withNavigation返回undefined

问题描述 投票:0回答:1

在我的react-native应用程序中,我有一个带有后退按钮的标题的组件。它的工作原理除了我单击后退按钮时它给了我这个错误:undefined不是一个对象(评估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);
reactjs react-native mobile react-navigation
1个回答
1
投票

您正在包装的组件是一个功能组件,因此您需要以props.foo而不是this.props.foo的形式访问您的道具。

onPress={() => props.navigation.goBack()}>

应该工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.