0

我是 React Native 的新手,我正在尝试使用这个包https://github.com/LiuC520/react-native-scrollable-tab-view-forked来使用标签,这就是标签的创建方式

<ScrollableTabView

   renderTabBar={() => (
       <ScrollableTabBar
       style={styles.scrollStyle}
            tabStyle={styles.tabStyle}
          />
        )}
        tabBarTextStyle={styles.tabBarTextStyle}
        tabBarInactiveTextColor={'black'}
        tabBarActiveTextColor={'red'}
        tabBarUnderlineStyle={styles.underlineStyle}
        initialPage={2}
      >

        <View key={'1'} tabLabel={'firt tab '} style={{flex:1,backgroundColor:'red'}}/>
        <View key={'2'} tabLabel={'second tab'} style={{flex:1,backgroundColor:'blue'}}/>
        <View key={'3'} tabLabel={'third tab'} style={{flex:1,backgroundColor:'yellow'}}/>
</ScrollableTabView>

它工作正常

但是当我试图渲染视图以在我失败的每个选项卡中显示它时,我已经尝试过这种方式

<View key={'1'} tabLabel={'firt tab '} style={{flex:1,backgroundColor:'red',height:100}}>
    <View><Text>first</Text></View>
</View>

但它给出了错误“TypeError:JSON.stringify 无法序列化循环结构”,我不是这个意思,如果有人已经使用过这个包或者知道解决方案是什么,请帮忙。

这是我的全部代码

import React from 'react';
import { ScrollView, ImageBackground, Image, View, StyleSheet, 
StatusBar, Dimensions, Platform } from 'react-native';
import { Block, Button, Text, theme } from 'galio-framework';
import ScrollableTabView, { DefaultTabBar, ScrollableTabBar } from 'react-native-scrollable-tab-view-forked';

const { height, width } = Dimensions.get('screen');
import { Images, argonTheme } from '../constants/';
import { HeaderHeight } from "../constants/utils";
import ajax from '../services/FetchSubjects';
import Loader from '../components/Loader';

const URI = 'http://localhost:8000';

export default class SubjectDetails extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
     loading: true,
     data: {
      subject: {},
      meals: []
    }
  }
 }  
 async componentDidMount() {
  const data = await ajax.fetchSubjectDetails(this.props.navigation.state.params.subjectId);
this.setState({
  data: {
    subject: data.subject,
    meals: data.meals
  },
  loading: false
 });
}

render() {
  const { navigation } = this.props;

  return (
   <Block safe style={styles.container}>
    <Block>
      <StatusBar barStyle="light-content" />
      <Loader loading={this.state.loading} />
      <Block flex style={styles.category}>
        <ImageBackground
          source={this.state.loading ? Images.Pro : { uri: URI + this.state.data.subject.cover.url }}
          style={{ flex: 1, height: 160, width, zIndex: 1 }}
        >
        <Block style={styles.categoryBg}>
          <Block style={styles.categoryTitle}>
            <Text size={18} bold color={theme.COLORS.WHITE}>
              {this.state.data.subject.name}
            </Text>
          </Block>
        </Block>
        </ImageBackground>
      </Block>
    </Block>
    <Block style={{marginTop:160,height:"100%"}}>
      <ScrollView>
        <ScrollableTabView

            renderTabBar={() => (
              <ScrollableTabBar
                style={styles.scrollStyle}
                tabStyle={styles.tabStyle}
              />
            )}
            tabBarTextStyle={styles.tabBarTextStyle}
            tabBarInactiveTextColor={'black'}
            tabBarActiveTextColor={'#2B4D8E'}
            tabBarUnderlineStyle={styles.underlineStyle}
            initialPage={2}
          >

          <View key={'1'} tabLabel={'firt tab '} style={{flex:1,backgroundColor:'red',height:100}}><Text>Lorem ipsum</Text></View>
          <View key={'2'} tabLabel={'second tab'} style={{flex:1,backgroundColor:'blue',height:100}}/>
          <View key={'3'} tabLabel={'third tab'} style={{flex:1,backgroundColor:'yellow',height:100}}/>
          <View key={'4'} tabLabel={'Fourth tab'} style={{flex:1,backgroundColor:'yellow',height:100}}/>
        </ScrollableTabView>
      </ScrollView>
    </Block>
  </Block>
  );
 }
}
4

1 回答 1

0

好的,我阅读了react-native-scroll-tab-viewreact-native-scroll-tab-view-forked代码,发现有些代码不同,这导致了你的问题。你应该使用react-native-scroll-tab-view.
原因是:首先我们看源码,位于index.js中的ScrollTabview,我只展示核心代码。

// this is react-native-scroll-tab-view code
let tabBarProps = {
      goToPage: this.goToPage,
      tabs: this._children().map((child) => child.props.tabLabel), //this is the diffent
      activeTab: this.state.currentPage,
      scrollValue: this.state.scrollValue,
      containerWidth: this.state.containerWidth,
    };
// this is the fork project
const tabBarProps = {
      goToPage: this.goToPage,
      tabs: this._children().map(child => child.props), //this is the diffent
      activeTab: this.state.currentPage,
      scrollValue: this.state.scrollValue,
      // containerWidth: this.props.containerWidth,
      containerWidth: this.props.tabsWidth || this.props.containerWidth,
      initialPage:this.props.initialPage,
    }
// the tabs in the fored is not according by tablabel.

// then it will call the this.renderTabBar method
return <View style={[styles.container, this.props.style, ]} onLayout={this._handleLayout}>
      {this.props.tabBarPosition === 'top' && this.renderTabBar(tabBarProps)}
      {this.renderScrollableContent()}
      {(this.props.tabBarPosition === 'bottom' || overlayTabs) && this.renderTabBar(tabBarProps)}
    </View>;

//in this.renderTabBar, you define the method, so it will call this.props.renderTabBar
renderTabBar(props) {
    if (this.props.renderTabBar === false) {
      return null;
    } else if (this.props.renderTabBar) {
      return React.cloneElement(this.props.renderTabBar(props), props);
    } else {
      return <DefaultTabBar {...props} />;
    }
  },

 // then, we look at the ScrollTabBar soruce code, which will render tabs
 //here it call the JSON.stringfy, so here it is your errors occur
componentDidUpdate(prevProps) {
    // If the tabs change, force the width of the tabs container to be recalculated
    if (JSON.stringify(prevProps.tabs) !== JSON.stringify(this.props.tabs) && this.state._containerWidth) {
      this.setState({ _containerWidth: null, });
    }
  },

我希望它打电话帮助你!

于 2019-11-04T14:59:01.097 回答