在createMaterialTopTabNavigator上创建动态标签以进行反应导航

问题描述 投票:0回答:1

我正在使用react-navigation的createMaterialTopTabNavigator制作顶部标签导航器。我面临的问题是选项卡必须由API请求的响应数据决定。例如,我调用一个API来请求足球队列表,接收该列表,然后设置MaterialTopTabNvigator的选项卡。

我已经使用以下组件来制作导航器的标签:

class TabBarLabel extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return this.props.focused ? (
      <View style={lstyles.container_focused}>
        <Text style={lstyles.label_focused}>{this.props.name}</Text>
      </View>
    ) : (
      <View style={lstyles.container_blur}>
        <Text style={lstyles.label_blur}>{this.props.name}</Text>
      </View>
    );
  }
}

const FootballTeamNavigator = createMaterialTopTabNavigator(
  {
    teamA : {
      screen: AScreenComponent,
      navigationOptions: () => {
        return {
          title: 'teamA',
          tabBarLabel({focused}) {
            return <TabBarLabel focused={focused} name="teamA" />;
          }
        };
      }
    }
  },
  {
    initialRouteName: group.members[0].id,
    swipeEnabled: false,
    timingConfig: {
      duration: 1000,
    },
    tabBarOptions: {
      scrollEnabled: true,
      ...styles,
    },
  },
);

我想做的就像:

let teamList = {};

apiForGetFootballTeamList().then(response => {
  for (const team of response.data.list) {
    tempList[team.name] = team;
  }
});

createMaterialTopTabNavigator(
  {
    ...teamList
  },
  {
    initialRouteName: ...,
    ...
  }
);

但是我不知道如何使用组件等数据来更新标签。 (组件具有状态,如果我们更新状态,则组件也会更新)

有什么办法吗?

react-native react-navigation
1个回答
0
投票

当前版本的React Navigation不支持动态配置。您需要为该https://blog.expo.io/announcing-react-navigation-5-0-bd9e5d45569e]使用React导航5

© www.soinside.com 2019 - 2024. All rights reserved.