React Native中如何在Drawer.Screen的options内添加drawerIcon
解决Drawer.Screen同时设置drawerIcon和条件禁用侧滑的问题
你的问题出在当前的options函数只在特定条件下返回配置对象,其余情况无返回值,导致drawerIcon这类全局配置无法生效。正确做法是始终返回完整的配置对象,在里面根据条件动态修改swipeEnabled属性。
修改后的代码示例:
<Drawer.Screen name="Home" component={StackNavigator} options={({route}) => { const routeName = getFocusedRouteNameFromRoute(route) ?? 'Billing'; // 定义包含drawerIcon的基础配置 const baseOptions = { drawerIcon: ({focused, color, size}) => ( // 替换为你需要的图标组件,示例用Ionicons的Home图标 <Ionicons name={focused ? 'home' : 'home-outline'} size={size} color={color} /> ), }; // 根据条件合并swipeEnabled配置 if (sideMenuDisabledScreens.includes(routeName)) { return {...baseOptions, swipeEnabled: false}; } // 其他场景返回基础配置 return baseOptions; }} />
关键说明:
- 先创建包含
drawerIcon的基础配置对象,确保所有场景下该配置都能生效 - 使用扩展运算符(
...)合并基础配置与条件性的swipeEnabled属性,避免覆盖原有配置 - 若需添加其他全局配置(如
title),直接写入baseOptions即可
内容的提问来源于stack exchange,提问作者Vishali
相关产品推荐
相关产品推荐

