Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-lazily

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-lazily

React.lazy wrapper that works with allows you to destruct imported module, so it will work with non default exports

  • 0.9.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
28K
increased by12.05%
Maintainers
1
Weekly downloads
 
Created
Source

react-lazily

minzip size install size dependency count

react-lazily is a simple wrapper around React.lazy (or loadable from @loadable/component) that supports named imports.

Usage

Consider a component MyComponent that is exported with a default export:

export default MyComponent;

Per React docs, you could use React.lazy as follows:

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

However, if the component is exported with a named export:

export const MyComponent = ...

You would have to use React.lazy like this:

But if the component is exported with named export export const MyComponent = ... then you have to do:

const MyComponent = React.lazy(() =>
  import('./MyComponent').then((module) => ({ default: module.MyComponent }))
);

With react-lazily it becomes:

const { MyComponent } = lazily(() => import('./MyComponent'));

Full example

See the live example: https://codesandbox.io/s/react-lazily-example-p7hyj

import React, { Suspense } from 'react';
import { lazily } from 'react-lazily';

const { MyComponent } = lazily(() => import('./MyComponent'));

const App = () => {
  const [open, setOpen] = React.useReducer(() => true, false);

  return (
    <>
      {open ? (
        <Suspense fallback={<div>Loading...</div>}>
          <MyComponent />
        </Suspense>
      ) : (
        <button onClick={setOpen}>Load</button>
      )}
    </>
  );
};

Full Example with @loadable/component

Don't forget to install @loadable/component first.

import React from 'react';
import { loadable } from 'react-lazily/loadable';

const { MyComponent } = loadable(() => import('./MyComponent'), {
  fallback: <div>Loading...</div>,
});

const App = () => {
  const [open, setOpen] = React.useReducer(() => true, false);

  return (
    <>
      {open ? <MyComponent /> : <button onClick={setOpen}>Load</button>}
    </>
  );
};

Keywords

FAQs

Package last updated on 09 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc