首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用react-native-tab-view显示动态标签?

如何使用react-native-tab-view显示动态标签?
EN

Stack Overflow用户
提问于 2018-09-03 17:26:27
回答 1查看 4.4K关注 0票数 6

我一直在尝试用react-native-tab-view显示动态标签,但它总是给我这样的错误:

代码语言:javascript
复制
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `SceneComponent`.

This error is located at:
    in SceneComponent (at SceneMap.js:16)
    in RCTView (at View.js:43)
    in AndroidViewPager (at ViewPagerAndroid.android.js:247)
    in ViewPagerAndroid (at PagerAndroid.js:154)
    in PagerAndroid (at TabView.js:59)
    in RCTView (at View.js:43)
    in RCTView (at View.js:43)
    in TabView (at UnderlineTabbar.js:76)

我的代码是:

代码语言:javascript
复制
import * as React from 'react';
import { StyleSheet, Dimensions, View } from 'react-native';
import {
  TabView,
  TabBar,
  SceneMap,
  type Route,
  type NavigationState,
} from 'react-native-tab-view';
import LinearGradient from 'react-native-linear-gradient';
import CategoryPage from './CategoryPage';
import GestureRecognizer, {swipeDirections} from 'react-native-swipe-gestures';
import ButtonWithIcon from '../../components/ButtonWithIcon';

type State = NavigationState<
  Route<{
    key: string,
    title: string,
  }>
>;

const initialLayout = {
  height: 0,
  width: Dimensions.get('window').width,
};

export default class UnderlineTabbar extends React.Component<*, State> {

  constructor(props) {
      super(props);

      this.state = {
        index: 0,
        routes: [],
        scenes: {}
      };
      props.categories.forEach(category => {
        this.state.routes.push({ key: category.description, title: category.name });
      });
      let scenes = {};
      props.categories.forEach(category => {
        if(category.assets.length > 0) {
          const FirstRoute = () => (
            <View style={[styles.container, { backgroundColor: '#ff4081' }]} />
          );
          scenes[category.description] = FirstRoute;
        }
      });
      this.state.scenes = scenes;
  }

  _handleIndexChange = index =>
    this.setState({
      index,
    });

  _renderTabBar = props => (
    <TabBar
      {...props}
      scrollEnabled
      indicatorStyle={styles.indicator}
      style={styles.tabbar}
      tabStyle={styles.tab}
      labelStyle={styles.label}
    />
  );

  render() {
    const config = {
      velocityThreshold: 0.1,
      directionalOffsetThreshold: 800
    };
    return (
      <TabView
        style={[styles.container, this.props.style]}
        navigationState={this.state}
        renderScene={SceneMap(this.state.scenes)}
        renderTabBar={this._renderTabBar}
        onIndexChange={this._handleIndexChange}
        initialLayout={initialLayout}
      />
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  tabbar: {
    backgroundColor: '#3f51b5',
  },
  tab: {
    width: 120,
  },
  indicator: {
    backgroundColor: '#ffeb3b',
  },
  label: {
    color: '#fff',
    fontWeight: '400',
  },
});
EN

回答 1

Stack Overflow用户

发布于 2019-12-22 16:57:56

对于动态页签,只需在满足条件后设置路由值即可:

例如:我必须根据api响应中的数据可用性来设置Review,Gallery选项卡:示例:

代码语言:javascript
复制
 this.state = {
  index: 0,
  routes: []
}
 ------------------

<TabView
      navigationState={this.state}
      renderScene={this.renderScene}
      renderTabBar={this.renderTabBar}
      onIndexChange={index => {
        this.setState({ index });
      }}
      swipeEnabled={false}
    />
-------------------------


 setRoutes() {
 let tabArray=[];
 if(......condition...){
 tabArray.push({key:"Gallery",title:"Gallery",navigation: this.props.navigation})
 }
 if(......condition...){
 tabArray.push({key:"Review",title:"Review",navigation: this.props.navigation})
 }
 //set route here
 this.setState({
  routes: tabArray
 });
 }

就是这样!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52146831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档