1

我有以下组件。创建初始选项卡集非常有用。

import * as React from 'react';
import { TabBar, TabView } from 'react-native-tab-view';
import { CollectionList } from './components';

const renderTabBar = (props) => <TabBar {...props} scrollEnabled />;

const RarityTabs = ({ collectionId, rarities }) => {
  const rarityRoutes = rarities.map((rarity) => ({
    key: rarity.variant,
    title: rarity.quality,
    rarity,
  }));

  const [index, setIndex] = React.useState(0);
  const [routes, setRoutes] = React.useState(rarityRoutes);

  return (
    <TabView
      lazy
      navigationState={{ index, routes }}
      renderScene={({ route }) => (
        <CollectionList collectionId={collectionId} selectedRarity={route.rarity} />
      )}
      renderTabBar={renderTabBar}
      onIndexChange={setIndex}
    />
  );
};

export default RarityTabs;

但是,rarities可以更改,我想让选项卡路由创建相应地响应。
当我尝试useEffect触发setRoutes时,它会锁定应用程序。

您如何为路线动态创建一种方式?谢谢!

也发布在 GitHub 上

4

1 回答 1

0
import * as React from 'react';
import {View, StyleSheet} from 'react-native';
import {TabView, TabBar, SceneMap} from 'react-native-tab-view';
import {connect} from 'react-redux';
import Categories from './Categories';

export default class CategoriesScrollable extends React.Component {
  constructor(props) {
    super(props);

    const {monthList, selected} = props;

    this.state = {
      index: selected,
      screens: {},
      routes: monthList,
    };
  }

  componentDidMount() {
    let screens = {};

    for (let i = 0; i < this.state.routes.length; i++) {
      screens[[`${this.state.routes[i].key}`]] = Categories;
    }
    this.setScreen(screens);
  }

  setScreen = (param) => {
    this.setState({screens: SceneMap(param)});
  };

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

  renderTabBar = (props) => (
      <TabBar {...props} scrollEnabled />
  );

  render() {
    return (
        <View style={{flex: 1, backgroundColor: Color.white}}>
          {this.state.screens.length > 0 && (
              <TabView
                  ref={this.props.innerRef}
                  lazy={true}
                  swipeEnabled={false}
                  navigationState={this.state}
                  renderScene={this.state.screens}
                  renderTabBar={this.renderTabBar}
                  onIndexChange={this.handleIndexChange}
              />
          )}
        </View>
    );
  }
}
于 2021-10-25T11:45:20.187 回答