是否可以将goBack()函数作为参数传递

时间:2020-04-10 17:32:36

标签: react-native react-navigation

我有一个AccountScreen功能。在此屏幕上,我希望能够navigate.goBack()。我尝试按照指南here进行操作,以了解如何返回上一屏幕,但出现错误。

这是我的代码:

AccountScreen.js

const BackButton = styled.Button`

`;


function AccountScreen({doThis}) {
    return(
        <Root>
            <BackButton title="" onPress={() => doThis} />
            <Text>Account</Text>
        </Root>
    )
};

export default AccountScreen;

navigations.js

const createProfileDrawer = ({navigation}) => 
            <Drawer.Navigator>
                <Drawer.Screen name="Profile" component={ProfileScreen}/>
                <Drawer.Screen name="Account" 
                component={AccountScreen(navigation.goBack())} />
            </Drawer.Navigator>

因此,当我尝试像这样传递它时,出现错误The action 'GO_BACK' was not handled by any navigator.是否可以传递像这样的函数?

1 个答案:

答案 0 :(得分:0)

尝试进行以下更改:

AccountScreen.js中:

import { useNavigation } from '@react-navigation/native';

function AccountScreen() {
    const navigation = useNavigation();

    return(
        <Root>
            <BackButton title="" onPress={() => navigation.openDrawer()} />

navigations.js中:

<Drawer.Screen name="Account" component={AccountScreen} />

请注意,您想要执行的操作可能不是“ goBack”,而是“ openDrawer”。