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

时间:2018-09-03 09:26:27

标签: javascript reactjs react-native react-native-tab-view

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

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)

我的代码是:

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',
  },
});

1 个答案:

答案 0 :(得分:0)

对于动态标签,仅在满足条件后设置路由值:

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

import tkinter as tk
from tkinter import ttk

def bar(event=None):
    print("Called bar")

def foo(event=None):
    print("Called foo")

root = tk.Tk()

# create a ttk menubutton
mb = ttk.Menubutton(root, text="File")

# create a cascade of menu options for this menubutton mb
mb.menu = tk.Menu(mb, tearoff=0)

mb.menu.add_command(label="Something else", command=bar, accelerator="Control-s")
root.bind("<Control-s>", bar)

mb.menu.add_command(label="Delete a thing",command=foo, accelerator="Delete")
root.bind("<Delete>", foo)

mb.configure(menu=mb.menu)
mb.pack(side='left')

root.mainloop()

就是这样!