简体   繁体   English

反应本机选项卡导航 - 选项卡更改被阻止,直到获取完成

[英]React native tab navigation - tab change blocked until fetch is complete

I have a react native tab navigation (with material tabs).Inside that tab navigation there are 2 tabs that both do initial data fetch when the tab is activated only once(the first render) and when the data page is changed(infinite scroll).我有一个React Native Tab导航(带有材料选项卡)。在该选项卡导航中,当仅激活一次选项卡时(第一个渲染)和更改数据页面时,都有2个选项卡进行初始数据获取(Infinite Scroll) .

When first tab is activated and data fetching is in progress, I can't switch to second tab until fetch is over.When i click on the second tab nothing happens but the click is somehow registered, and tab is switched with a delay as soon as the fetch on the previous tab is completed.当第一个选项卡被激活并且数据获取正在进行时,我无法切换到第二个选项卡,直到获取结束。当我点击第二个选项卡时没有任何反应但点击以某种方式注册,并且选项卡尽快切换延迟因为上一个选项卡上的提取已完成。

My question is, can this tab change be ublocked when fetching data in one of the tabs is in progress.我的问题是,当正在其中一个选项卡中获取数据时,是否可以取消阻止此选项卡更改。

Here's the code这是代码

Initial fetch, the same effect is applied when user scrolls to the bottom to load new data(infinite scroll)初始获取,当用户滚动到底部以加载新数据时应用相同的效果(无限滚动)

  useEffect(() => {
        dispatch({ name: 'spinner' });
        fetch(route.params.apiUrl + items.page).then((res) => {
            if (!res.ok) {
                throw new Error('Nije OK Status')
            }
            return res
        }).then((promise) => promise.json()).then((data) => {
            dispatch({ name: 'load', data: data })
        })
    }, [items.page])

function that increments page number, that triggers loading new data function 增加页码,触发加载新数据

const loadMore = () => {
        dispatch({ name: 'page increment' })
    }

FlatList平面列表

 <>
            {items.isLoading ? <View style={styles.indicator}><ActivityIndicator size="large" color={constants.secondary} /></View> : null}
            <FlatList numColumns={1}
                onEndReached={loadMore}
                onEndReachedThreshold={0.5}
                initialNumToRender={10}
                maxToRenderPerBatch={30}
                data={items.data}
                keyExtractor={(item, index) => item.id}
                renderItem={renderItem}
                removeClippedSubviews={true}
                ListHeaderComponent={renderHeader}
            />
        </>

You can use loading state while fetch is working and disable tab while loading is true (there must be 'disabled' property for Tab component).您可以在提取工作时使用加载 state,并在加载为真时禁用选项卡(Tab 组件必须有“禁用”属性)。

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

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