在onPress HeaderBackButton内找不到变量导航

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

我正在尝试覆盖HeaderBackButton,并且我已经成功做到了,但是问题是我不能在HeaderBackButton onPress中使用goBack,我无法访问onPress内的导航对象HeaderBackButton的按钮。

我正在使用这段代码

static navigationOptions = {
    title: 'Operation Screen',
    headerLeft: (
        <HeaderBackButton
            onPress={ ()=>{
                    console.log('HeaderBackButton');
                    // Do some custom stuff
                    navigation.goBack();
                }}
        />
    )
};
react-native react-navigation react-navigation-stack
1个回答
0
投票

只需将其定义为一个函数。您可以将navigation作为参数

static navigationOptions = ({ navigation }) => {
    return {
        title: 'Operation Screen',
        headerLeft: (
            <HeaderBackButton
                onPress={() => {
                        console.log('HeaderBackButton');
                        // Do some custom stuff
                        navigation.goBack();
                    }}
            />
        )
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.