【问题标题】:React-Native: components only working properly on the last tab loaded in when using react-native-navigation or react-navigationReact-Native:组件仅在使用 react-native-navigation 或 react-navigation 时加载的最后一个选项卡上正常工作
【发布时间】:2017-06-03 17:18:57
【问题描述】:

我有一个非常简单的两个标签应用程序:

import React, { Component } from 'react';
import { AppRegistry } from 'react-native';
import { TabNavigator } from 'react-navigation';

import {
  Sports,
  News,
} from 'externalComponents';

const MyApp = TabNavigator({
  Sports: { screen: Sports },
  News: { screen: News },
});

AppRegistry.registerComponent('MyApp', () => MyApp);

体育和新闻组件由另一家公司提供,我无法编辑该代码。如果我将它加载到 TabBarIOS 组件中,一切正常,所有选项卡都按预期工作。但是,对于 react-native-navigationreact-navigation,只有最后一个加载的选项卡才能正常工作。

NewsSports 组件都像这样加载 JSONDownloadService 组件:

downloadService = new JSONDownloadService(this);

然后它调用这个:

downloadService.getJSON(this.state.url, type)

现在在 JSONDownloadService 中,因为它已在其构造函数中传递了 NEWSSPORTS 组件,因此它被传递了 updateComponent(未正确调用的部分)。 getJSON() 看起来像这样:

getJSON(url, type) {
  //set up vars...
    fetch(request, init)
        .then(function (response) {
          // ...some code
        })
        .then(function (response) {
            if (response) {
                try {
                 // supposed to call `updateComponent` in News & Sports:
                    callback.updateComponent(response, type);
                }
                catch (err) {
                    console.log(err);
                }
            }
        })
        .catch(function (error) {
            console.error(error);
        });
}

问题是,updateComponent() 只会被 tabBar 中加载的最后一个组件调用。如果我改变他们的位置,只有最后一个有效。除了,如果我在最后一个选项卡中注释掉与JSONDownloadService 相关的代码,那么第一个可以正常工作。似乎无论哪个组件最后使用它都会阻止其余组件更新。如何使用react-native-navigationreact-navigation 完成这项工作?

提前感谢您的帮助!

【问题讨论】:

    标签: javascript react-native tabs react-navigation react-native-navigation


    【解决方案1】:

    事实证明,TabBarIOS 有效而 react-navigationreact-native-navigation 无效的原因是因为后两者同时加载了所有选项卡。在这种情况下,它重载了 JSONDownloadService 组件。

    我至少可以使用以下代码在react-navigation 中修复它:

    const MyApp = TabNavigator({
      Sports: { screen: Sports },
      News: { screen: News },
    }, {
      lazy: true, //used to be called "lazyLoad"
    });
    

    它只会导致应用延迟加载每个选项卡的内容。

    【讨论】:

    • 这还能用吗?在我的代码中,lazy 或 lazyload 对 react native 的最新版本没有任何影响(所有控制器都在应用程序启动时加载)
    猜你喜欢
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2019-03-19
    • 2017-10-24
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多