Reactjs React Naitve导航:如何显示抽屉中的模式?
有没有办法在React Native中显示抽屉中的模态? 有一个问题,我根据答案更改了代码 我现在的代码是这样的Reactjs React Naitve导航:如何显示抽屉中的模式?,reactjs,react-native,react-native-navigation,Reactjs,React Native,React Native Navigation,有没有办法在React Native中显示抽屉中的模态? 有一个问题,我根据答案更改了代码 我现在的代码是这样的 MyRootStackNavigator.tsx const Stack = createStackNavigator<RootStackParamList>(); const MyRootStackNavigator = () => { return ( <Stack.Navigator mode="modal">
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>
);
};
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
和部分。如何设置节以在抽屉上显示模式?您可以使用内部具有模式的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>
);
}
功能自定义抽屉内容(道具){
const[modalVisible,setModalVisible]=使用状态(false);
返回(
setModalVisible(假)}>
这里的模态内容
setModalVisible(false)}/>
{
setModalVisible(真);
}}
/>
);
}
抽屉显示器应该这样设置
<Drawer.Navigator
drawerContent={(props) => <CustomDrawerContent {...props} />}>
}>
你可以在这里查阅文件
如果您想继续使用您在问题中输入的代码,您可以像下面那样导航到about屏幕(而不是在内容中使用模式)
{
props.navigation.navigate('About')
}}
/>
您可以在您的抽屉导航器中再添加一个
,或者第二种方法是在我添加时创建一个自定义抽屉内容
@KartikeyVaish,模式显示,但它显示在一个空白屏幕上,正如这个问题所说的那样。@Ooto你想有一个按钮导航到吗?@GuruparanGiritharan否我想做的是在抽屉上放置一个链接,如Open model
,但当我将它添加到抽屉中时,模式显示在一个空白屏幕上。好的,如果你需要抽屉里的一个按钮,可以很容易地打开,我会发布一个答案。我会很快尝试,让你知道!当然,用另一个选项更新了答案。你可以选择任何你喜欢的选项。我不想导航到其他屏幕。我只想通过单击抽屉上的一个部分来打开一个模式。当我将一个模式组件添加到Drawer.Screen
时,模式会显示出来,但看起来像是导航到一个空白屏幕。无论如何,我会尝试你的解决方案。好的,然后使用自定义内容选项!它工作得很好!谢谢!
<DrawerItem
label="Open Screen"
onPress={() => {
props.navigation.navigate('About')
}}
/>