简体   繁体   English

我应该使用 react-loadable 还是 loadable-components 进行代码拆分?

[英]Should I use react-loadable or loadable-components for code splitting?

I want to split my react code with server side rendering.我想用服务器端渲染拆分我的反应代码。 To do that I have two options.为此,我有两个选择。

  • loadable-components可加载组件
  • react-loadable反应加载

loadable-components可加载组件

React documentation suggested to use loadable-components for server rendered apps. React 文档建议对服务器渲染的应用程序使用可加载组件 But it has very few NPM weekly downloads.但它每周的 NPM 下载量很少。

react-loadable反应加载

NPM weekly downloads of this package is very high compared to the previous one but according to loadable-components documentation this package is not maintained any more.与之前的相比,该软件包的 NPM 每周下载量非常高,但根据loadable-components文档,该软件包不再维护。

react-loadable was the recommended way for React code splitting for a long time. react-loadable 长期以来一直是 React 代码拆分的推荐方式。 However, today it is not maintained any more and it is not compatible with Webpack v4+ and Babel v7+.但是,今天它不再维护,并且与 Webpack v4+ 和 Babel v7+ 不兼容。 Documentation Link 文档链接

Please guild me with proper package.请用适当的包裹来引导我。

Even though documentation of react-loadable says that react-loadable is not compatible with Webpack v4+ and Babel v7+, I used react-loadable and it worked.尽管react-loadable 的文档说react-loadable与 Webpack v4+ 和 Babel v7+ 不兼容,但我使用了react-loadable并且它有效。 I did not face any issue in both server and client side rendering applications.我在服务器端和客户端渲染应用程序中都没有遇到任何问题。

FWIW loadable-components is recommended by the React team React 团队推荐 FWIW loadable-components

Also, React.lazy does not currently support SSR (as of May 2020)此外, React.lazy目前不支持 SSR(截至 2020 年 5 月)

you can use React.lazy .你可以使用React.lazy This will automatically load the bundle containing the OtherComponent when this component gets rendered.当这个组件被渲染时,这将自动加载包含 OtherComponent 的包。

React.lazy takes a function that must call a dynamic import(). React.lazy 采用一个必须调用动态 import() 的函数。 This must return a Promise which resolves to a module with a default export containing a React component.这必须返回一个 Promise,该 Promise 解析为具有包含 React 组件的默认导出的模块。

const OtherComponent = React.lazy(() => import('./OtherComponent'));

function MyComponent() {
  return (
    <div>
      <OtherComponent />
    </div>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM