【问题标题】:How do you create dynamic/update-able routes with react-native-tab-view?如何使用 react-native-tab-view 创建动态/可更新路由?
【发布时间】:2021-04-14 18:42:34
【问题描述】:

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

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 时,它会锁定应用程序。

如何创建动态路由方式?谢谢!

Also posted on GitHub

【问题讨论】:

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


    【解决方案1】:
    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-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    相关资源
    最近更新 更多