React Native:更改后 TabView 不重新呈现

React Native: TabView not re-rendering after change

我正在尝试动态更改 TabView 的内容。基本上,当用户打开应用程序时,屏幕会获取用户帐户并为用户拥有的每种不同货币呈现一个选项卡。 (因此,如果用户有 10 个美元账户,则只会显示一个美元选项卡,但如果他有 1 个美元和 1 个欧元帐户,则会显示一个美元和一个欧元选项卡)。

当用户打开应用程序时一切正常,但是当他创建或删除帐户时,TabView 不会更新并且显示旧状态或者如果新状态的货币数量少于以前的状态则崩溃。

 private _renderCardsTabs = () => {
    const { accounts: { accounts } } = this.props;
    console.log("debug _renderCardTabs", accounts)

    if (accounts.length === 0 || accounts.length < 1) {
      return this._renderEmptyCardsTabs();
    }

    let scenes = this._renderTabs()

    return (
      <TabView
        navigationState={this.state}
        renderTabBar={this._renderTabBar}
        renderScene={({ route }) => {
          return scenes[route.key]()
        }}
        onIndexChange={this._tabHandler}
      />
    );
  }

      private _renderTabBar = (props) => {
    return (
      <View style={styles.tabBarWrapper}>
        <TabBar
          {...props}
          indicatorStyle={styles.indicator}
          style={styles.tabBar}
          labelStyle={styles.label}
          scrollEnabled={false}
          pressOpacity={1}
        />
      </View>
    )

      private _renderTabs = () => {
    const { accounts: { accounts } } = this.props;

    return [...new Set(accounts.map((account: any) => account.currency_symbol.split('-')[1].toLowerCase()))]
      .reduce((acc, item) => {
        acc[item] = () => {
          const { navigation } = this.props;
          return <CardsScreen navigation={navigation} currency={item.toUpperCase()} />;
        };
        return acc;
      }, {});
  }

错误

TypeError: scenes[route.key] is not a function

我尝试过的东西(我使用了 SceneMap)

最终导航状态没有更新,我需要用最新的路线更新它,这应该通过 React Component 生命周期正确完成,但现在只需手动设置它 this.state.routes = this._getRoutes() 解决问题。

 private _renderCardsTabs = () => {
    const { accounts: { accounts } } = this.props;

    if (accounts.length === 0 || accounts.length < 1) {
      return this._renderEmptyCardsTabs();
    }

    let scenes = this._renderTabs()
    // This should not go here but it works, I need to update this.state after accounts
    // are updated by sagas from somewhere that doesn't cause a rerender 
    this.state.routes = this._getRoutes()

    return (
      <TabView
        navigationState={this.state}
        renderTabBar={this._renderTabBar}
        renderScene={({ route }) => {
          return scenes[route.key]()
        }}
        onIndexChange={this._tabHandler}
      />
    );
  }