keepcalculator

06 Numbers and Powers

Loading Indicator Flicker: Fix Delay or Boundary?

Card No. 06·78770 · Updated · Standard formula, shown below

If your loading indicator flickers, the first thing to check is the suspense boundary—not the delay. A flickering spinner is almost always a sign that the component suspends and resolves too quickly, causing React to unmount and remount the fallback repeatedly. Adding a small delay can mask the symptom, but fixing the boundary placement or the data-fetching logic addresses the root cause.

What Causes a Flickering Loading Indicator

In React, when a component suspends (for example, while waiting for data), React looks for the nearest <Suspense> boundary. If that boundary has a fallback (like a spinner), it shows the fallback. When the promise resolves, React tries to render the component again. If the component suspends again immediately—because the data isn’t actually cached or the promise is recreated on each render—the fallback will flash on and off. This is the classic flicker.

Another common cause is a race condition: the loading state toggles to true and then back to false within the same frame, so the user sees a brief flash. This often happens when the data is already in a cache but the code still sets a loading flag before checking the cache.

How the Suspense Boundary Works

A suspense boundary is a React component that wraps parts of the tree. When any child suspends, React shows the boundary’s fallback instead of the suspended part. The boundary does not “wait” for a minimum time—it shows the fallback as soon as a child suspends, and hides it as soon as the child resolves. If the child resolves and then suspends again (even milliseconds later), the fallback reappears. That is why a flicker often indicates that the boundary is too close to the component that is suspending, or that the promise is not stable.

For example, if you have:

<Suspense fallback={<Spinner/>}>
  <Profile />
</Suspense>

and Profile suspends on a fetch that resolves quickly, the spinner may flash. If the fetch is inside a useEffect that runs after the first render, the component will render once (without suspending), then suspend on the second render, causing a double render and a flicker.

Why Adding a Delay Might Help (and Why It Might Not)

Some developers add a minimum display time to the spinner (e.g., 300 ms) to avoid a flash. This works if the flicker is caused by a very fast resolve—the spinner stays visible long enough for the eye to register it, and it disappears smoothly. But if the flicker is caused by a re-suspend, a delay only makes the spinner appear, disappear, and then reappear after the delay, which looks even worse.

A delay can also hurt perceived performance: if the data actually arrives in 50 ms, forcing the spinner to stay for 300 ms makes the app feel slower. The user waits longer for content that was already ready.

How to Fix the Flicker at the Source

Here are the steps to eliminate flicker without relying on a delay:

  • Move the suspense boundary up. Wrap a larger section (e.g., the whole page) with one boundary instead of putting a boundary around each small component. This way, when one child suspends, the fallback covers the whole area, and when it resolves, the whole area updates at once—no partial flicker.
  • Ensure the promise is stable. If you create a new promise on every render (e.g., by calling a fetch function inside the component body), React will suspend every time. Use a module-level cache or a library like React Query to deduplicate requests and return the same promise for the same key.
  • Check for double-fetch. In React 18 StrictMode, effects run twice in development. If your fetch is in an effect, the first fetch may be cancelled, but the loading state might toggle twice. Use a ref to ignore the second invocation.
  • Use useTransition for non-urgent updates. If the loading is triggered by a state update (like switching tabs), wrap the update in startTransition. React will keep showing the old UI while the new one is being prepared, and only show the fallback if the transition takes too long.
  • Set a minimum display time only as a last resort. If you cannot fix the root cause, use a custom hook that keeps the spinner visible for at least 200 ms after it first appears. But this is a band-aid, not a cure.

Common Pitfalls and Misconceptions

One common mistake is to put a setTimeout inside the fallback component to delay its unmount. That does not work because React will unmount the fallback as soon as the suspended child resolves, regardless of timers. Another misconception is that Suspense has a built-in “minimum display time” prop—it does not. You have to implement that yourself if you want it.

Also, be aware that a flicker can be caused by a missing key on a list. If you re-render a list and the keys change, React unmounts and remounts the list items, which can trigger a loading state if the list item suspends. Always use stable, unique keys.

When to Use a Delay vs. Fix the Boundary

Use a delay only when the flicker is purely a visual artifact of a very fast fetch (under ~100 ms) and you cannot avoid the brief loading state. For example, if you are fetching from a local cache and the promise resolves in 10 ms, a 200 ms minimum display time will smooth it out. But if the flicker happens because the boundary is too small or the promise is unstable, fix those first.

In general, the rule is: delay the indicator, not the data. Never artificially delay the actual data resolution—that makes the app slower. Only delay the display of the spinner.

Frequently Asked Questions

Why does my spinner flash even when the data is cached?

If the data is cached but your code still sets a loading state to true before checking the cache, the spinner will show for one frame. Fix this by checking the cache first and only setting loading to true if the data is not present.

Does Suspense have a built-in delay prop?

No. Suspense only accepts fallback and children. There is no delayMs or similar prop. You must implement a delay yourself if you want one.

What is the difference between useTransition and Suspense?

useTransition lets you mark a state update as non-urgent. React will continue showing the old UI while the new UI is being rendered, and only show the fallback if the render takes too long. Suspense is the mechanism that shows a fallback when a component suspends. They are often used together: useTransition for the update, Suspense for the fallback.

Can a flicker cause layout shift or performance issues?

Yes. If the spinner appears and disappears rapidly, it can cause layout shift if the spinner has a different size than the content. It also wastes CPU cycles on unnecessary renders. Fixing the flicker improves both visual stability and performance.

Is it okay to use a CSS animation to fade in the spinner?

Yes, a fade-in can make the appearance less jarring, but it does not fix the underlying flicker. If the spinner mounts and unmounts repeatedly, the fade-in will restart each time, which can look like a strobe effect. Fix the boundary first, then consider a fade-in as a polish.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →