【问题标题】:Closing Drawer On Logout in React Native在 React Native 中注销时关闭抽屉
【发布时间】:2020-09-01 22:22:18
【问题描述】:

在我的React Native 应用程序中,我有一个处理登录过程的StackNavigator 和一个可供经过身份验证的用户使用的DrawerNavigator

当我退出应用程序然后重新登录时,我看到抽屉仍然打开。当我单击注销时,我在Navigator 组件中调用handleClickLogOut() 函数——见下文。我想通过访问MainMenuDrawer,我可以访问closeDrawer() 方法,但我在那里看不到。

如何在此代码中注销时关闭抽屉?

这是Navigator 组件:

const LogInStack = new createStackNavigator();
const MainMenuDrawer = createDrawerNavigator();

class Navigator extends Component {

    constructor(props) {
        super(props);
        this.handleClickLogOut = this.handleClickLogOut.bind(this);
    }

    componentDidMount() {

        if (this.props.isAuthenticatedUser && !this.props.isAuthenticated)
            this.props.actions.setIsAuthenticated(true);
    }

    handleClickLogOut() {

        removeAuthenticationData()
            .then(() => {

                return this.props.actions.setIsAuthenticated(false);
            });
    }

    render() {
        return (
            <NavigationContainer>
                {
                    this.props.isAuthenticated
                    ? <MainMenuDrawer.Navigator drawerContent={(navigation) => <DrawerContent member={this.props.member} navigation={navigation} handleClickLogOut={this.handleClickLogOut} />}>
                        <MainMenuDrawer.Screen name="Home" component={Dashboard} />
                        <MainMenuDrawer.Screen name="ToDoList" component={ToDoList} />
                      </MainMenuDrawer.Navigator>
                    : <LogInStack.Navigator screenOptions={{ headerShown: false }}>
                        <LogInStack.Screen name="LoginScreen" component={Login} />
                      </LogInStack.Navigator>
                }
            </NavigationContainer>
        );
    }
}

【问题讨论】:

  • 如果从 DrawerContent 传递一个回调函数到 handleClickLogOut 并调用呢?那么它将在导航上下文中对吗?

标签: react-native react-navigation react-navigation-stack react-navigation-drawer


【解决方案1】:

@Sam 这里是解决方案兄弟。

import {DrawerActions} from '@react-navigation/native'

然后你可以在 DrawerContent.js 文件中调用:

navigation.dispatch(DrawerActions.closeDrawer())

希望这会帮助你?。

这是我在 DrawerContent.tsx 文件中的真实代码

【讨论】:

  • 我认为他的问题是从导航上下文之外的 handleClickLogOut 调用 closedrawer
  • @GuruparanGiritharan 你是正确的兄弟。没错,我建议他调用 DrawerContent.js 文件
猜你喜欢
  • 2021-08-23
  • 2016-12-22
  • 2019-03-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多