reactjs - React Naitve Navigation:如何从抽屉中显示模式?
问题描述
有没有办法在 React Native 中从抽屉中显示模式?有一个类似的问题,我按照答案更改了我的代码。
我现在的代码是这样的。
MyRootStackNavigator.tsx
const Stack = createStackNavigator<RootStackParamList>();
const MyRootStackNavigator = () => {
return (
<Stack.Navigator mode="modal">
<Stack.Screen
name="Main"
component={MyDrawerNavigator}
options={{ headerShown: false }}
/>
<Stack.Screen
name="MyModal"
component={MyModal}
/>
</Stack.Navigator>
);
};
MyDrawerNavigation.tsx
const Drawer = createDrawerNavigator();
const MyDrawerNavigator = () => {
return (
<Drawer.Navigator>
<Drawer.Screen
name="Home"
component={MyStackNavigator}
/>
<Drawer.Screen
name="About this app"
component={About}
/>
</Drawer.Navigator>
);
}
但是对于此代码,显示模式的部分不会出现在抽屉上。只有Home
和About this app
部分出现在抽屉上。如何设置部分以在抽屉上显示模式?
解决方案
您可以使用内部有模态的 CustomDrawerContent 并使用按钮打开模态
function CustomDrawerContent(props) {
const [modalVisible, setModalVisible] = useState(false);
return (
<DrawerContentScrollView {...props}>
<Modal
style={{ flxe: 1, backgroundColor: 'red' }}
visible={modalVisible}
onRequestClose={() => setModalVisible(false)}>
<Text>Modal Content Here</Text>
<Button title="Close" onPress={() => setModalVisible(false)} />
</Modal>
<DrawerItemList {...props} />
<DrawerItem
label="Open Modal"
onPress={() => {
setModalVisible(true);
}}
/>
</DrawerContentScrollView>
);
}
DrawerNavigator 应该像这样设置
<Drawer.Navigator
drawerContent={(props) => <CustomDrawerContent {...props} />}>
您可以在此处参考文档 https://reactnavigation.org/docs/drawer-navigator/#providing-a-custom-drawercontent
如果您想继续使用您在问题中输入的代码,您可以像下面那样导航到关于屏幕(而不是在内容中包含模式)
<DrawerItem
label="Open Screen"
onPress={() => {
props.navigation.navigate('About')
}}
/>
推荐阅读
- c++ - 如何进行无分支数字循环?
- c++ - QGridLayout 小部件设置相等大小
- sql - 如果 count(column) 然后在 Oracle 中选择
- c# - 在 Cefsharp.WPF 中,我应该如何对“fi-FI”(芬兰语)语言进行拼写检查?
- python - 我在 Pygame 中收到一个关于字体的表面错误。我该如何解决?
- str-replace - 使用 str_replace 函数回显错误数据
- javascript - 获取“仅对页面组件执行导出的查询”。在 Gatsby 中尝试生成页面时
- flutter - 使用 Flutter 和 Dart 实现多个过滤器
- laravel - 如何编写更改迁移以在 laravel 中添加复合键
- php - 来自 ABAP Web 服务的响应返回多个项目的数组,但返回单个项目的对象