项目作者: ganapativs

项目描述 :
Dynamically load and render any react module(Component or an HOC) using dynamic import 🎉
高级语言: JavaScript
项目地址: git://github.com/ganapativs/react-dynamic-import.git
创建时间: 2018-10-26T11:26:43Z
项目社区:https://github.com/ganapativs/react-dynamic-import

开源协议:MIT License

下载


react-dynamic-import

⚠️ You might not need this library. Checkout React.lazy and see if it fits your use case.

Dynamically load and render any react module(Component or an HOC) using dynamic import 🎉

Tiny(around 1.16kb gzip) dynamic module loader and renderer.

👉 DEMO

⚠️ Hooks only(requires react 16.8.0 or above), use v1.0.4 if you want older react versions support

Dynamic loading of component is already supported in React using React.lazy and Suspense, But, dynamic loading of HOC is tricky and is unsupported in React.

Should work with any bundler(eg: webpack, parcel etc) which supports dynamic import ✨

MIT Licence
Open Source Love
Build Status
npm version
GitHub version Greenkeeper badge

Table of Contents

Install

NPM

  1. npm install react-dynamic-import

Yarn

  1. yarn add react-dynamic-import

UMD build

  1. <script src="https://unpkg.com/react-dynamic-import/dist/react-dynamic-import.umd.js"></script>

Basic usage

  1. Component

    • Folder structure

      1. |_ realComponent.js
      2. |_ container.js <-- working file
    • Usage

      1. // Import library
      2. import ReactDynamicImport from "react-dynamic-import";
      3. // or const ReactDynamicImport = require('react-dynamic-import');
      4. // Define dynamic import loader function
      5. const loader = () => import(`./realComponent.js`);
      6. /**
      7. * Use dynamic module and lazy fetch component
      8. *
      9. * Make sure to use it outside render method,
      10. * else new component is rendered in each render
      11. *
      12. * You can choose to show a placeholder and render
      13. * error component in case of error, check API section for more
      14. */
      15. const RealComponent = ReactDynamicImport({ loader });
      16. class Container extends React.component {
      17. render() {
      18. /**
      19. * This component is dynamically fetched and rendered
      20. * on first usage/render
      21. */
      22. return <RealComponent ></RealComponent>;
      23. }
      24. }
  2. HOC

    • Folder structure

      1. |_ realComponent.js <-- Real component to wrap in HOC
      2. |_ withHOC.js <-- HOC
      3. |_ container.js <-- working file
    • Usage

      1. // Import library
      2. import ReactDynamicImport from "react-dynamic-import";
      3. // or const ReactDynamicImport = require('react-dynamic-import');
      4. import RealComponent from "./realComponent.js";
      5. // Define dynamic import loader function
      6. const loader = () => import(`./withHOC.js`);
      7. /**
      8. * Use dynamic module and lazy fetch HOC
      9. *
      10. * Make sure to use it outside render method,
      11. * else new component is rendered in each render
      12. *
      13. * You can choose to show a placeholder and render error
      14. * component in case of error, check API section for more
      15. */
      16. const DynamicHOC = ReactDynamicImport({ loader, isHOC: true });
      17. const WrappedComponent = DynamicHOC(RealComponent);
      18. class Container extends React.component {
      19. render() {
      20. /**
      21. * The actual HOC is lazy loaded and executed,
      22. * which in turn renders the actual component lazily
      23. */
      24. return <WrappedComponent ></WrappedComponent>;
      25. }
      26. }

Advanced usage

  1. Component

    • Folder structure

      1. |_ dynamic
      2. | |_ realComponent-en.js
      3. | |_ realComponent-eu.js
      4. | |_ realComponent-kn.js
      5. |_ container.js <-- working file
    • Usage

      1. // Import library
      2. import ReactDynamicImport from "react-dynamic-import";
      3. // or const ReactDynamicImport = require('react-dynamic-import');
      4. /**
      5. * Define dynamic import loader function
      6. *
      7. * This loads specific module from many available
      8. * modules in the directory, using given module name
      9. */
      10. const loader = f => import(`./dynamic/${f}.js`);
      11. // Use dynamic module and lazy fetch component
      12. class Container extends React.component {
      13. constructor(props) {
      14. super(props);
      15. /**
      16. * Make sure to use it outside render method, else
      17. * new component is rendered in each render
      18. *
      19. * You can choose to show a placeholder and render error
      20. * component in case of error, check API section for more
      21. *
      22. * This loads different module when different language
      23. * configuration is passed
      24. */
      25. this.RealComponent = ReactDynamicImport({
      26. name: `realComponent-${props.lang || "en"}`,
      27. loader
      28. });
      29. }
      30. render() {
      31. const { RealComponent } = this;
      32. /**
      33. * This component is dynamically fetched and rendered
      34. * on first usage/render
      35. */
      36. return <RealComponent ></RealComponent>;
      37. }
      38. }
  2. HOC

    • Folder structure

      1. |_ dynamic <-- Dynamic HOC's
      2. | |_ withHOC-en.js
      3. | |_ withHOC-eu.js
      4. | |_ withHOC-kn.js
      5. |_ realComponent.js <-- Real component to wrap in HOC
      6. |_ container.js <-- working file
    • Usage

      1. // Import library
      2. import ReactDynamicImport from "react-dynamic-import";
      3. // or const ReactDynamicImport = require('react-dynamic-import');
      4. import RealComponent from "./realComponent.js";
      5. /**
      6. * Define dynamic import loader function
      7. *
      8. * This loads specific module from many available
      9. * modules in the directory, using given module name
      10. */
      11. const loader = f => import(`./dynamic/${f}.js`);
      12. // Use dynamic module and lazy fetch component
      13. class Container extends React.component {
      14. constructor(props) {
      15. super(props);
      16. /**
      17. * Make sure to use it outside render method, else
      18. * new component is rendered in each render
      19. *
      20. * You can choose to show a placeholder and render error
      21. * component in case of error, check API section for more
      22. *
      23. * This loads different module when different language
      24. * configuration is passed
      25. */
      26. const DynamicHOC = ReactDynamicImport({
      27. name: `withHOC-${props.lang || "en"}`,
      28. loader,
      29. isHOC: true
      30. });
      31. this.WrappedComponent = DynamicHOC(RealComponent);
      32. }
      33. render() {
      34. const { WrappedComponent } = this;
      35. /**
      36. * The actual HOC is lazy loaded and executed,
      37. * which in turn renders the actual component lazily
      38. */
      39. return <WrappedComponent ></WrappedComponent>;
      40. }
      41. }

Checkout API for more info.

API

Contribute

Thanks for taking time to contribute, please read docs and checkout src to understand how things work.

Reporting Issues

Found a problem? Want a new feature? First of all see if your issue or idea has already been reported.
If don’t, just open a new clear and descriptive issue.

Submitting pull requests

Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.

  • Fork it!
  • Clone your fork: git clone https://github.com/<your-username>/react-dynamic-import
  • Navigate to the newly cloned directory: cd react-dynamic-import
  • Create a new branch for the new feature: git checkout -b my-new-feature
  • Install the tools necessary for development: yarn
  • Make your changes.
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request with full remarks documenting your changes

TODO

  • Test cases

License

MIT License © Ganapati V S