gpt4 book ai didi

react-native - 如何在 react 导航 5x 中隐藏抽屉项目?

转载 作者:行者123 更新时间:2023-12-03 14:37:54 29 4
gpt4 key购买 nike

我正在尝试使用 react-navigation 在我的应用程序中创建一个抽屉导航,但我很难隐藏一个项目。
我想要的是:创建一些屏幕(在应用程序内部导航),但不在抽屉中显示这些屏幕。
我正在使用这个文档:( https://reactnavigation.org/docs/nesting-navigators/#navigator-specific-methods-are-available-in-the-navigators-nested-inside )

但我有两个问题:1) Root仍在显示中; 2)我无法直接导航到“隐藏”屏幕,它说该屏幕不存在。

这是我当前的代码:

const Stack = createStackNavigator();
function Root2() {
return (
<Stack.Navigator>
<Stack.Screen name="NewEditPilot" component={NewEditPilot} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
);
}


const Drawer = createDrawerNavigator();

function MyDrawer() {
return (
<Drawer.Navigator drawerContent={props => <SideBar2 {...props} />}>
<Drawer.Screen name="Home" component={Home} />
<Drawer.Screen name="PilotMgnt" component={PilotMgnt} />
<Drawer.Screen name="Root2" component={Root2} />
</Drawer.Navigator>
);
}


export default function App() {


return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<Root>
<NavigationContainer>
<MyDrawer />
</NavigationContainer>
</Root>
</PersistGate>
</Provider>
);
}

在这种情况下,我试图隐藏 NewEditPilotSettings屏幕。这是我导航到这些屏幕的方式: navigation.navigate('NewEditPilot') (这是在 react-navigation 4x 上工作的)。

另外,这就是我在 react-navigation 4x 中使用(和工作!)的方式:
// Telas principais
const Drawer = DrawerNavigator(
{
Home: { screen: Home },
PilotMgnt: { screen: PilotMgnt},
CurRace: { screen: CurRace},
Settings: { screen: Settings},
LogViewScreen: { screen: LogViewScreen},
},
{
initialRouteName: "Home",
contentOptions: {
activeTintColor: "#e91e63"
},
contentComponent: props => <SideBar {...props} />
}
);

// sub-telas
const AppNavigator = StackNavigator(
{
Drawer: { screen: Drawer },
NewEditPilot: { screen: NewEditPilot },
PersonalRank: { screen: PersonalRank },
RCharts: { screen: RCharts},
RChartsArchive: { screen: RChartsArchive},
Archive: { screen: Archive },
ArchiveView: { screen: ArchiveView },
ArchivePersonalRank: { screen: ArchivePersonalRank },
},
{
initialRouteName: "Drawer",
headerMode: "none"
}
);




export default () =>
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<Root>
<AppNavigator />
</Root>
</PersistGate>
</Provider>
;

最佳答案

this issue 的帮助下,我设法正确解决了这个问题.
要正确隐藏项目,您应该创建一个过滤路由的函数,并将其传递给 抽屉内容 的 Prop 抽屉.导航 .
假设您想隐藏 登录 从您的抽屉中筛选出来,但您仍然希望能够导航到它,无论何时,您都应该执行以下操作:

import {
createDrawerNavigator,
DrawerContentScrollView,
DrawerItemList,
} from '@react-navigation/drawer';

const Drawer = createDrawerNavigator();

const CustomDrawerContent = (props) => {
const { state, ...rest } = props;
const newState = { ...state };
newState.routes = newState.routes.filter(
(item) => item.name !== 'Login',
);

return (
<DrawerContentScrollView {...props}>
<DrawerItemList state={newState} {...rest} />
</DrawerContentScrollView>
);
};

export default () => (
<Drawer.Navigator
initialRouteName="Home"
drawerContent={(props) => <CustomDrawerContent {...props} />}
screenOptions={{
headerShown: true,
}}>
<Drawer.Screen
name="Home"
component={HomeScreen}
options={{ title: 'Home' }}
/>
.
.
.
希望这有帮助,干杯。

关于react-native - 如何在 react 导航 5x 中隐藏抽屉项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62204060/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com