Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Lazy and delaying the load of the component to fall in Suspense

Tags:

reactjs

I want to experiment with Suspense and Lazy. For this, I was thinking in defer o delay the load of the component for certain time, however, I could not do it.

What I was doing is using setTimeout() at componenDidMount(), then update the state and return that piece of state. But, this is not working as I expected.

Can anyone help me delaying the load of a component with Lazy without using browsers tools to fall in Suspense fallback?

like image 736
Peter Avatar asked Sep 03 '25 01:09

Peter


1 Answers

If I understood your question right, something like this should work for you:

const Component = React.lazy(async () => {
  await new Promise(resolve => setTimeout(resolve, YOUR_DELAY));
  return import('./Component');
});
like image 67
Berouminum Avatar answered Sep 09 '25 09:09

Berouminum