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

@thejohnfreeman/loadable

v1.1.3

Published

Like react-loadable, but better.

Downloads

51

Readme

loadable

Like react-loadable, but better.

npm bundle size code style: Prettier build status

Motivation

react-loadable has some problems. As of this writing, there are 39 open pull requests and issues are closed. The creator and maintainer seems to have abandoned the project, perhaps in favor of @loadable/component, but perhaps because of his emotional instability.

The general idea of react-loadable is that you give it an async function, load, that returns a component type (not a component or element), Target, and it will give you a new component type, Proxy. The first time an element of Proxy is rendered, it will save its props, call load, and return a placeholder "loading" element while it waits. Once load returns with Target, the proxy component will update itself with an element of type Target, passing along the props it saved.

What I wanted initially, was the ability to use the props in my load function, but even @loadable/component has that. What I want now is to pass an async function that takes the props and returns an element, not just the component type. Then I can use it for fetching resources, not just for code-splitting.

Usage

Code splitting:

import loadable from '@thejohnfreeman/loadable'
const Proxy = loadable()(() => import('./Target'))
// <Proxy />

Resource loading:

import loadable from '@thejohnfreeman/loadable'
const AsyncProduct = loadable()(async ({ productId }) => {
  try {
    const product = await backend.getProduct(productId)
  } catch (error) {
    return <Error error={error} />
  }
  return <Product product={product} />
})
// <AsyncProduct productId={productId} />

Options

Options are passed to the call to loadable, not to the function it returns, which takes the load function.

  • delay :: number

    Number of milliseconds to wait before showing the placeholder (to avoid a flicker of content). The default is 200.

  • Placeholder :: React.ComponentType

    The placeholder component type to show while waiting for the target. It will be given the same props as the target, in case you want to make use of them. The default is the text "Loading...".