react-native:react-navigation抽屉标签

问题描述 投票:0回答:1

我想在抽屉导航器中有抽屉标签/分隔符。

Somewhat like this

我该怎么做?

reactjs react-native react-navigation
1个回答
2
投票

简单。你在看什么叫做contentComponent。基本上你需要在你的抽屉导航器中注入一个contentComponent道具。

contentComponent用于呈现抽屉内容的组件,例如导航项。接收抽屉的导航道具。 Read more here

import DrawerContent from '../app/components/DrawerContent'
const drawerConfiguration = {
    initialRouteName: 'MainStackNavigatior',
    headerMode: 'screen',
    drawerWidth: deviceWidth / 1.38,
    contentComponent: DrawerContent
}

其中contentComponent只是一个包含可定制项目列表的ScrollView

class DrawerContent extends Component {
  onItemPress(item) {
    const { navigation } = this.props;
    navigation.navigate(item.key);
  }

  renderDrawerItem(route) {
    const { drawerItems } = this.props;
    if (drawerItems.indexOf(route.key) > -1) {
      return (
        <TouchableOpacity style={styles.drawerItem} key={route.key} onPress={() => this.onItemPress(route)}>
          <Text>{route.routeName}</Text>
        </TouchableOpacity>
      );
    }
    return null;
  }

  render() {
    const { navigation, isFetching, drawerItemsTitle } = this.props;
    return (
      <View style={styles.container}>
        {!isFetching && <View style={styles.drawerItemTitle}>
          <Text style={styles.drawerItemTitleText}>{drawerItemsTitle}</Text>
        </View>}
        {!isFetching && <View>
          {navigation.state.routes.map(route => this.renderDrawerItem(route))}
        </View>}
        {isFetching && <View style={styles.spinnerViewBg}>
          <View style={styles.spinner}>
            <ActivityIndicator
              size="small"
              animating
            />
          </View>
        </View>}
      </View>
    );
  }
}

这是Infinite Red的一个很好的例子。 Tutorial link

至于分离器,它基本上是一个具有最小高度和一定宽度的View。我相信你可以搞清楚:)祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.