async-library / react-async

🍾 Flexible promise-based React data loader

Home Page:https://docs.react-async.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Best way to perform a silent refresh

austingayler opened this issue · comments

Say you have something like:

const logs = useAsync(fetchLogs);

if(logs.isPending) return <p>Loading...</p>

return
<h1>Logs</h1>
{logs.data.map(log => <p>{log.text}</p>)}

However I also have it on an interval to refresh every n seconds. As is, I can call logs.reload(), but this flashes "Loading" every time it is refreshing. I see I can do fetchLogs().then(resp => logs.setData(resp.data)) but this feels a bit hacky. Is there a more elegant way to silently refresh the data without resetting the state?

Yes, there's the initial flag on <IfPending> and the persist flag on <IfFulfilled> to do this. However this requires that you use these helper components. If you don't want to do that you can just look at the data property directly, which is what the helpers do.

Cheers, thanks for the answer.