【发布时间】: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-navigation 和 react-navigation,只有最后一个加载的选项卡才能正常工作。
News 和 Sports 组件都像这样加载 JSONDownloadService 组件:
downloadService = new JSONDownloadService(this);
然后它调用这个:
downloadService.getJSON(this.state.url, type)
现在在 JSONDownloadService 中,因为它已在其构造函数中传递了 NEWS 或 SPORTS 组件,因此它被传递了 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-navigation 或react-navigation 完成这项工作?
提前感谢您的帮助!
【问题讨论】:
标签: javascript react-native tabs react-navigation react-native-navigation