我正在尝试动态更改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)
答案 0 :(得分:0)
最终,导航状态没有更新,我需要使用最新的路线对其进行更新,这应该在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}
/>
);
}