繁体   English   中英

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

[英]React Native: TabView not re-rendering after change

我试图动态更改TabView的内容。 基本上,当用户打开应用程序时,屏幕将获取用户帐户并为用户拥有的每种不同的货币呈现一个选项卡。 (因此,如果用户有10个USD帐户,则只会显示“ USD”标签,但是如果他有1 USD和1 EUR帐户,则会显示“ USD”和“ EUR”标签)。

当用户打开应用程序时,一切正常,但是当用户创建或删除帐户时,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}
      />
    );
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM