javascript - 如何使 SectionList 仅在拉动刷新时显示刷新指示器
问题描述
我有一个SectionList
我想做拉刷新的地方。我也有一个DataContext
提供:
fetching: boolean
, 表示正在获取某物的状态fetchedData: Data[]
, 包含从 fetch 中存储的数据的状态fetchData: () => Promise<void>
获取数据并将其存储在本地的函数。它将fetching
状态更新true
为false
- 有一个被触发并在is时从数据库
useEffect
执行读取setFetchedData
fetching
false
- 有一个
useEffectPolling
它是一个自定义包装器,它提供类似可取消的东西setInterval
,如果它已经处于活动状态,它可以取消一个承诺。它基本上fetchData
每分钟运行一次。
我的SectionList
样子
<SectionList
refreshControl={
<RefreshControl
refreshing={fetching}
onRefresh={fetchData}
/>
}
</SectionList>
发生的事情是刷新图标每分钟出现一次(这是我的代码所期望的)。但是,我想要的是在后台更新时以某种方式不显示它。但是,如果我拉动刷新,仍然会显示它。
的代码DataContext
类似于以下内容:为简洁起见,我跳过了一些清理代码并创建了一些可取消的承诺
export function DataContextProvider({children}) {
const [fetching, setFetching] = useState<boolean>(true);
const [fetchedData, setFetchedData] = useState<Data[]>([]);
useEffect(()=>{
(async () => {
// fetch remote and store to local db
await service.fetchAndStore()
setFetching(false);
})()
}, []);
useEffect(()=>{
if (fetching) {
console.log("still fetching don't do anything")
return () => {}
}
const cancelablePromise = cancelable(async () => {
// updates the state with the stored data
setFetchedData(await service.getStoredData());
})());
return () => cancelablePromise.cancel();
}, [fetching]);
const fetchData = async () => {
setFetching(true)
await service.fetchAndStore()
setFetching(false)
}
// This is a wrapper that executes something every few seconds something like
// a smarter setInterval. I don't think it is relevant to the question.
useEffectPolling(fetchData, 60000);
return <DataContext.Provider value={{
fetching,
fetchData,
fetchedData,
}}>
{children}
</DataContext.Provider>
}
我的猜测,虽然我不确定调用第二个状态backgroundFetch
并将其用作内部状态是否是个好主意。但我担心重新渲染的性能。
解决方案
推荐阅读
- c# - Did the behaviour of deleted files open with FileShare.Delete change on Windows?
- sql-server - Tableau - 未显示具有“用户定义的表类型”的 SQL Server 存储过程
- npm - vscode terminal crashes on .nprmc env config
- python - 如何在 python 数据框中随机化而不重复?
- python - 在 Tkinter 中加载页面
- python - Google Cloud Vision 搜索产品 Python 客户端 - 如何返回 10 多个结果?
- vue.js - 如何在 vue.js 中传递对象道具数组?
- google-contacts-api - 谷歌支付问题
- python - 自定义 Six.moves 模块的自省
- jq - jq select with multiple conditions