Skip to content

RTKQ: Data stays as old value and loading immediately jumps back to false when fetching fresh data from different endpoint + query param combination #1339

@agusterodin

Description

@agusterodin

Hey, I am experiencing an issue when using the useQuery auto-generated hook. If I change query parameters, the data remains the same (I expect it to be undefined so I can show loading placeholder) and loading jumps from "true" immediately back to "false" despite the data for that given combination not being loaded yet. I have keepUnusedDataFor set to 0 btw (not sure if relevant or not).

The docs claim:

isLoading refers to a query being in flight for the first time for the given endpoint + query param combination. No data will be available at this time.

This is really hard to reproduce in a Codepen since it relies on the server having some sort of delay. I wasn't aware of this bug until I used an endpoint that is extremely slow (4+ sec response time).

Hopefully this attached video, code snippet, and console log demonstrate the issue effectively. Please let me know if there are any other details I can provide!!

Notice that despite the argument changing from "monthly" to "weekly" it is still providing the "monthly" data instead of data being undefined while still loading.

Component

import { useSelector } from 'react-redux'
import MetricGraphs from './MetricGraphs'
import MetricStats from './MetricStats'
import { getOverviewMetricTimeframe } from 'state/campaigns/overview/overview'
import { useGetAggregateCampaignMetricsQuery } from 'api/campaign'
import MetricTimeframeSelector from './MetricTimeframeSelector'

export default function Metrics() {
  const currentlySelectedTimeframe = useSelector(getOverviewMetricTimeframe)
  const { data, isLoading, originalArgs } = useGetAggregateCampaignMetricsQuery(currentlySelectedTimeframe)
  console.table({ currentlySelectedTimeframe, originalArgs, isLoading, data })

  return (
    <>
      <div className="relative">
        <MetricTimeframeSelector />
      </div>
      <div className="relative grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-4 mt-5">
        <MetricStats metricResults={data} />
        <MetricGraphs metricResults={data} />
      </div>
    </>
  )
}

Recreation In Browser
https://user-images.githubusercontent.com/10248395/126681813-9656f46e-46a8-4a2e-b316-9229291699b5.mov

Metadata

Metadata

Assignees

No one assigned

    Labels

    RTK-QueryIssues related to Redux-Toolkit-QueryenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions