birdofpreyru/react-global-state

A problem with data refresh by useAsyncData()

Closed this issue · 1 comments

Here is the deal: if the component using the hook is re-rendered for a reason unrelated to this hook after the data maxage has passed, the data refresh won't be triggered automatically, as the refresh is protected by useEffect(..). I believe, it needs to be handled as a special case: if there are previously loaded data, but they are stale according to maxage, the re-loading should be attempted, assuming it is permitted by refreshAge.

useEffect(() => {
const state = globalState.get(path);
if (refreshAge < Date.now() - state.timestamp && !state.operationId) {
load(path, loader, globalState);
}
}, options.deps || []);
}
return {
data: maxage < Date.now() - localState.timestamp ? null : localState.data,
loading: Boolean(localState.operationId),
timestamp: localState.timestamp,
};

On the other hand, it was more like a deliberate design decision: if something leads to the component update, that should be mentioned as dependency, and then it will work. Need an extra thought about it. Probably, introduce different component modes, which do this with different logic?