react-native - 将自定义列表传递给 React Native 导航 5.5 DrawerItemList

标签 react-native react-navigation react-navigation-drawer

我需要使用 React Native 为应用程序制作多级抽屉菜单。我可以使用 CustomDrawerContent 自定义抽屉。在此我想显示路线的子集。目前我正在制作 props 的副本并更改其中的 state.routes 变量。有更好的方法吗?

const Drawer = createDrawerNavigator();
function CustomDrawerContent(props) {  
  const prop2 = props;
  prop2.state.routes = prop2.state.routes.slice(0, 3);

  return (
    <DrawerContentScrollView {...props} contentContainerStyle={{ flex: 1 }}>
        <View style={styles.drawerMenu}>
            <DrawerItemList {...prop2} />
        </View>
    </DrawerContentScrollView>
  );
}

export default function () {
  return (
    <Drawer.Navigator initialRouteName="Home_Home" hideStatusBar={true} statusBarAnimation="fade" 
        drawerContent={(props) => <CustomDrawerContent {...props} />}>
      <Drawer.Screen name="Home_Home" component={HomeStack} />
      <Drawer.Screen name="Home_About" component={HomeStack} />
      <Drawer.Screen name="Home_FAQ" component={HomeStack} />
      <Drawer.Screen name="Profile_Calendar" component={HomeStack} />
      <Drawer.Screen name="Profile_Subscription" component={HomeStack} />
    </Drawer.Navigator>
  );
}

我尝试过将状态作为 Prop 传递,但似乎没有成功。

最佳答案

您可以将 DrawerItemList 替换为 react-navigation repository 中的实际代码。然后过滤掉你想要/不想要的路线,如下所示:

  const buildLink = useLinkBuilder()

  // Add your logic here
  const visibleRoutes = ['home', 'profile', 'resources', 'faq']
  
  const drawerList = props.state.routes.map((route: any, i: number) => {
    const focused = i === props.state.index
    const { title, drawerLabel, drawerIcon } = props.descriptors[route.key].options

    if (!visibleRoutes.includes(route.name)) {
      return <View key={route.key} />
    }

    return (
      <DrawerItem
        key={route.key}
        label={
          drawerLabel !== undefined
            ? drawerLabel
            : title !== undefined
            ? title
            : route.name
        }
        icon={drawerIcon}
        focused={focused}
        activeTintColor={props.activeTintColor}
        inactiveTintColor={props.inactiveTintColor}
        activeBackgroundColor={props.activeBackgroundColor}
        inactiveBackgroundColor={props.inactiveBackgroundColor}
        labelStyle={props.labelStyle}
        style={props.itemStyle}
        to={buildLink(route.name, route.params)}
        onPress={() => {
          props.navigation.dispatch({
            ...(focused
              ? DrawerActions.closeDrawer()
              : CommonActions.navigate(route.name)),
            target: props.state.key,
          })
        }}
      />
    )
  })

关于react-native - 将自定义列表传递给 React Native 导航 5.5 DrawerItemList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62179212/

相关文章:

react-native - 从抽屉打开时刷新组件( react 导航)

javascript - 无法将图片上传到 Firebase

react-native - react native ScrollView 类型错误 : undefined is not an object (evaluating 'this._subscribableSubscriptions.forEach' )

express - 使用expo表达服务器时没有请求头来源

javascript - react 导航未定义错误

reactjs - 如果应用程序关闭,则 React-Native 深层链接不起作用(React-Navigation v5)

javascript - react native : build Login Screen

javascript - 如何使用 React Navigation 在 React Native 中以编程方式在 Android 上隐藏标签栏?

javascript - react native 抽屉导航器未打开