我正在用React Navigation构建一个抽屉,并且如果用户关闭抽屉,想执行一些逻辑。我在文档中看不到任何明显的东西可以使我做到这一点。有人知道这样做的方法吗?

最佳答案

您需要自定义导航操作以捕获DrawerClose事件:

const MyDrawerNavigator = DrawerNavigator({
    //...
});

const defaultGetStateForAction = MyDrawerNavigator.router.getStateForAction;

MyDrawerNavigator.router.getStateForAction = (action, state) => {

    //use 'DrawerOpen' to capture drawer open event
    if (state && action.type === 'Navigation/NAVIGATE' && action.routeName === 'DrawerClose') {
        console.log('DrawerClose');
        //write the code you want to deal with 'DrawerClose' event
    }
    return defaultGetStateForAction(action, state);
};

09-26 10:29