所以这里是主要思想,HOC 能够使用 React.lazy 和 React.Suspense 加载任何包装在组件中的组件。可能吗???所以,我已经能够写一些,但不确定我是否能够正确地制作......
import React, { Suspense, lazy, useState, useEffect } from "react"
export function withLazyImport(LazyComponent) {
return (props) => {
const [loadedComponent, setLoadedComponent] = useState(null)
useEffect(() => {
setLoadedComponent(lazy(() => import(<LazyComponent {...props} />)))
//eslint-disable-next-line
}, [])
return (
<Suspense fallback="Lazy component is loading ...">
{loadedComponent}
</Suspense>
)
}
}