This package has been deprecated

Author message:

This package is now part of @darkobits/react-kit. See https://github.com/darkobits/react-kit for more.

@darkobits/react-lazy-named
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

  • Lazily import React components that are defined using named exports.
  • Supports destructuring syntax, including nesting.
  • 100% type-safe.

Install

$ npm install @darkobits/react-lazy-named

Use

Assuming we have a file Components.tsx that exports 2 React components and some other value:

Components.tsx

import React from 'react';

export const Foo = () => {
  return (
    <div>Foo</div>
  )
};

export const Bar = () => {
  return (
    <div>Bar</div>
  );
}

export const Baz = false;

Then, in another file, we can lazily import these components thusly:

App.tsx

import { lazy } from '@darkobits/react-lazy-named';
import React from 'react';

// Foo, Bar are correctly typed as lazy React components.
const { Foo, Bar } = lazy(async () => import('./Components.tsx'));

// Type error.
const { Baz } = lazy(async () => import('./Components.tsx'));

export const App = () => {
  return (
    <Foo />
    <Bar />
  )
}

Pre-Loading

This module attaches a static preload method to components that can be invoked to optimistically pre-load a component. This method returns a Promise that will resolve when the component has loaded.

import { lazy } from '@darkobits/react-lazy-named';

const { Foo } = lazy(async () => import('./Components.tsx'));

await Foo.preload();

Caveats

  • When using this (or other solutions) to circumvent the default export requirement, tree-shaking will not work on the imported module. In most cases, this should not be an issue.

Addendum


Readme

Keywords

none

Package Sidebar

Install

npm i @darkobits/react-lazy-named

Weekly Downloads

2

Version

0.2.1

License

WTFPL

Unpacked Size

6.92 kB

Total Files

6

Last publish

Collaborators

  • darkobits