npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

uselazy

v5.1.6

Published

react hook for lazy load and code-split react components or whatever you want.

Downloads

5

Readme

uselazy

react hook for lazy load and code-split react components or whatever you want.

NOTE: useLazy now handles both dynamic and named imports.

GitHub version Build Status Eclipse Marketplace GitHub last commit GitHub commit activity GitHub issues

Installation

 npm install uselazy

or

 yarn add uselazy

API

  // This it whats takes useLazy:
  useLazy<T>(
    // array of functions that returns a promise from a dynamic import which
    // could be an object with a default import or named import
    // NOTE: please you should wrap this value inside of `useMemo`
    importFns: Array<() => Promise<{ default: T } | { [K: string]: T }>>,
    // this is were you decided when to execute the import
    shouldImport: boolean
  );

Usage

handles default import

// Text.tsx
import React from 'react';

const Text = () => <p> Here's your beer </p>;

export default Text;

// App.tsx
import React, { useState, useMemo } from 'react';
import useLazy from 'uselazy';

const imports = [() => import('./Text')];

const App = () => {
  const [shouldImport, setShouldImport] = useState(false);
  const { isLoading, result } = useLazy(
    // Preserves identity of "imports" so it can be safely add as a dependency of useEffect
    // inside useLazy
    useMemo(() => imports, []),
    shouldImport,
  );

  const [TextComponent] = result;

  return (
    <div>
      <h1>I'm very lazy </h1>
      <button onClick={() => setShouldImport(!shouldImport)}>Buy me a beer</button>

      {isLoading && <span>some spinner</span>}

      {TextComponent && <TextComponent />}
    </div>
  );
};

handles named imports

// Bears.tsx
import React from 'react';

export const Bears = () => <p> Bears loves beers </p>;

// App.tsx
import React, { useState, useMemo } from 'react';
import useLazy from 'uselazy';

const namedImports = [() => import('./Bears')];

const App = () => {
  const [shouldImport, setShouldImport] = useState(false);
  const { isLoading, result } = useLazy(
    // Preserves identity of "namedImports" so it can be safely add as a dependency of useEffect
    // inside useLazy
    useMemo(() => namedImports, []),
    shouldImport,
  );

  const [BearsComponent] = result;

  return (
    <div>
      <h1>I'm very lazy </h1>
      <button onClick={() => setShouldImport(!shouldImport)}>Buy me a beer</button>

      {isLoading && <span>some spinner</span>}

      {BearsComponent && <BearsComponent />}
    </div>
  );
};

Or you can handle both default and named imports

// Text.tsx
import React from 'react';

const Text = () => <p> Here's your beer </p>;

export default Text;

// Bears.tsx
import React from 'react';

export const Bears = () => <p> Bears loves beers </p>;

// App.tsx
import React, { useState } from 'react';
import useLazy from 'uselazy';

const imports = [() => import('./Text'), () => import('./Bears')];

const App = () => {
  const [shouldImport, setShouldImport] = useState(false);
  const { isLoading, result: Components } = useLazy(
    // Preserves identity of "imports" so it can be safely add as a dependency of useEffect
    // inside useLazy
    useMemo(() => imports, []),
    shouldImport,
  );

  return (
    <div>
      <h1>I'm very lazy </h1>
      <button onClick={() => setShouldImport(!shouldImport)}>Buy me lots of beers</button>

      {isLoading && <span>some spinner</span>}

      {Components && Components.map(Component => <Component />)}
    </div>
  );
};

Or other stuff rather than React components

// someUtils.ts
import React from 'react';

const someUtils = {
  byMoreBeers(cuantity: number): string {
    return `${cuantity} beers on the way ;)`;
  },
};

export default someUtils;

// App.tsx
import React, { useState } from 'react';
import useLazy from 'uselazy';

const utilsImport = [() => import('./someUtils')];

const App = () => {
  const [shouldImport, setShouldImport] = useState(false);
  const { isLoading, result } = useLazy(
    // Preserves identity of "utilsImport" so it can be safely add as a dependency of useEffect
    // inside useLazy
    useMemo(() => utilsImport, []),
    shouldImport,
  );

  const [utils] = result;

  return (
    <div>
      <button onClick={() => setShouldImport(!shouldImport)}>Buy me lots of beers</button>

      {isLoading && <span>some spinner</span>}

      {utils && (
        <button onClick={() => utils.byMoreBeers(5)}>Buy me more beers for my friends!</button>
      )}
    </div>
  );
};

NOTE

The reason why I'm encouraging to wrap the imports array with useMemo is because of useEffect's array of dependencies, that triggers a re-render whatever they change, so if you DON'T wrap it, you'll get an infinite rerendering loop because, each render the imports array is different. [] === [] // false.

so I giving the developer total control of this, he decides whether the array can change.

more details here: A Complete Guide to useEffect

LICENSE

MIT