这里有一个例子:
import React, { lazy, Suspense } from 'react';
const LazyComponent = lazy(() => import('./LazyComponent'));
function MyComponent() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
</div>
);
}
在这个例子中,LazyComponent
只有在需要时才会被加载并在Suspense组件内显示。
fallback
prop用于指定组件加载时显示的内容。