【发布时间】:2017-03-06 15:58:30
【问题描述】:
我将 react-navigation https://reactnavigation.org/ 用于我的第一个 react-native 项目。我想切换我的抽屉(打开和关闭),但我不知道如何实现这一点。我一直在查看 reactnavigation.org 的文档,但找不到任何提示。
【问题讨论】:
-
你这样做了吗?
标签: react-native react-navigation
我将 react-navigation https://reactnavigation.org/ 用于我的第一个 react-native 项目。我想切换我的抽屉(打开和关闭),但我不知道如何实现这一点。我一直在查看 reactnavigation.org 的文档,但找不到任何提示。
【问题讨论】:
标签: react-native react-navigation
通过 React Navigation(v2)
要打开和关闭抽屉,请使用以下助手打开和关闭抽屉:
this.props.navigation.openDrawer();
this.props.navigation.closeDrawer();
如果您想切换抽屉,请调用以下命令:
this.props.navigation.toggleDrawer();
这些函数中的每一个都在幕后简单地调度操作:
this.props.navigation.dispatch(DrawerActions.openDrawer());
this.props.navigation.dispatch(DrawerActions.closeDrawer());
this.props.navigation.dispatch(DrawerActions.toggleDrawer());
【讨论】:
如果您想切换抽屉,您可以导航到“DrawerToggle”,这将根据抽屉当前状态选择适合您的导航。
相应地触发“DrawerOpen&DrawerClose”
this.props.navigation.navigate('DrawerToggle');
【讨论】:
根据the docs:
要打开和关闭抽屉,请分别导航到“DrawerOpen”和“DrawerClose”。
this.props.navigation.navigate('DrawerOpen'); // open drawer this.props.navigation.navigate('DrawerClose'); // close drawer
【讨论】:
查看 this.props.navigation.state。您应该能够在 example 中的 routeName 中找到“DrawerOpen”或“DrawerClose”。
【讨论】:
对于版本 5.x:
导入 DrawerActions:
import { DrawerActions } from "@react-navigation/native";
你可以使用navigation.dispatch(DrawerActions.toggleDrawer())
【讨论】: