Peter
Peter

Reputation: 2394

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

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?

Upvotes: 4

Views: 3079

Answers (1)

Berouminum
Berouminum

Reputation: 530

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');
});

Upvotes: 8

Related Questions