我试图创建LLC https://blog.logrocket.com/lazy-loading-components-in-react-16-6-6cea535c0b52/,但最终出现错误:
Objects are not valid as a React child (found: object with keys {$$typeof, _ctor, _status, _result}). If you meant to render a collection of children, use an array instead.
<LazyLoadingComponent resolve={() => import('../StaticComponents/Notification/Notification')}>
</LazyLoadingComponent>
import React, { lazy, Suspense } from 'react';
function LazyLoadingComponent({ resolve }) {
console.log('resolve', resolve);
return (
<Suspense fallback={() => <h1>loading</h1>}>
{lazy(async () => {
const { default: module } = await resolve();
console.log(module, 'module');
return module;
})}
</Suspense>
);
}
export default LazyLoadingComponent;
答案 0 :(得分:0)
尝试一下:
// App.js
import React, { lazy, Suspense } from "react";
// Import your compnent like so
const LazyComponent = lazy(() => import("./LazyComponent"))
function App() {
return (
<div className="App">
<h1>Testing Lazy component..</h1>
<Suspense fallback={<h2>See Loading?</h2>}>
<LazyComponent />
</Suspense>
</div>
);
}
您的惰性组件:
// LazyComponent.js
import React from "react";
export default function LazyComponent() {
return (
<div>
<h1>I'm Lazy</h1>
<p>
This is my lazy component!
</p>
</div>
);
}
codeSandbox示例。
答案 1 :(得分:0)
无论如何,感谢您的帮助!
<LazyLoadingComponent
component={lazy(() => import('../StaticComponents/Notification/Notification'))}
></LazyLoadingComponent>
<Suspense fallback={() => <h1>loading</h1>}>
<Component></Component>
</Suspense>