Skip to content
Advertisement

React suspense/lazy delay?

I am trying to use the new React Lazy and Suspense to create a fallback loading component. This works great, but the fallback is showing only a few ms. Is there a way to add an additional delay or minimum time, so I can show animations from this component before the next component is rendered?

Lazy import now

JavaScript

Waiting component:

JavaScript

Can I do something like this?

JavaScript

Advertisement

Answer

lazy function is supposed to return a promise of { default: ... } object which is returned by import() of a module with default export. setTimeout doesn’t return a promise and cannot be used like that. While arbitrary promise can:

JavaScript

If an objective is to provide minimum delay, this isn’t a good choice because this will result in additional delay.

A minimum delay would be:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement