Skip to content

RTKQ: use forceRefetch to invalidate query data? #3996

Open
@MaximKondratev

Description

@MaximKondratev

I have some charts data that I want to invalidate after 180 seconds if query with associated arg called. keepUnusedDataFor does not suit this case as it invalidates data only if it was not used for the time passed. I want to invalidate it no matter if used or not.

I am trying to do this by forceRefetch:

const CHART_TTL_MS = 1000 * 180;

forceRefetch: ({ endpointState }) => {
    if (!endpointState) {
        return true;
    }

    const startedTimeStamp = endpointState.startedTimeStamp;
    const nowTimeStamp = new Date().getTime();

    if (nowTimeStamp - startedTimeStamp > CHART_TTL_MS) {
        return true;
    }

    ...
}

This actually works, it starts refetching, but I see "previous" data rendered for a moment before state isFetching switching to true and I can render some loading view . I could not find any flag or something else to not render that previous data or any way to understand it is going to refetch.

Is there something I missed? Or is it impossible to do invalidation this way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions