gpt4 book ai didi

state - React Native : Force reload of TabBarIOS. 项目

转载 作者:行者123 更新时间:2023-12-03 03:59:09 26 4
gpt4 key购买 nike

我在 React Native 中有一个基于 TabBar 的应用程序。多个选项卡使用相同的数据源(AsyncStorage)。如果我现在更新一个选项卡中的数据并打开另一个选项卡,则会显示旧数据。我不知道如何在每次项目激活时强制重新加载。

  1. FavoritesView:显示保存的数据
  2. ExploreView:操作保存的数据
  3. FavoritesView:显示过期数据(--> 强制重新加载)

    <TabBarIOS.Item
    title="Explore"
    icon={{uri:'ic_explore'}}
    selected={this.state.selectedTab === 'exploreTab'}
    onPress={() => {
    this.setState({
    selectedTab: 'exploreTab'
    });
    }}>
    <ExploreView/>
    </TabBarIOS.Item>

    <TabBarIOS.Item
    title="Favorites"
    icon={{uri:'ic_favorite_border'}}
    selected={this.state.selectedTab === 'favoriteTab'}
    onPress={() => {
    this.setState({
    selectedTab: 'favoriteTab'
    });
    }}>
    // Reload this
    <FavoritesView/>
    </TabBarIOS.Item>

    <TabBarIOS.Item
    systemIcon="more"
    selected={this.state.selectedTab === 'moreTab'}
    onPress={() => {
    this.setState({
    selectedTab: 'moreTab'
    });
    }}>
    <MoreView/>
    </TabBarIOS.Item>

我已经尝试设置一个新状态来触发更新,但它似乎没有改变任何东西。

<TabBarIOS.Item
title="Favorites"
icon={{uri:'ic_favorite_border'}}
selected={this.state.selectedTab === 'favoriteTab'}
onPress={() => {
this.setState({
selectedTab: 'favoriteTab',
forceUpdate: Math.random()
});
}}>
<FavoritesView forceUpdate={this.state.forceUpdate}/>
</TabBarIOS.Item>

最佳答案

我遇到了类似的问题,最终对我有用的是重写嵌入 View 上的 componentWillReceiveProps 。每当 View 在 TabBarIOS 中设置为 selectedTab 时都会调用它。

https://facebook.github.io/react/docs/component-specs.html#updating-componentwillreceiveprops

关于state - React Native : Force reload of TabBarIOS. 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31015045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com