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

react-hoc-loading

v1.0.7

Published

HOC for React components that can be loading

Downloads

2

Readme

react-hoc-loading

Travis Coverage Status Maintainability Dependencies

HOC to set React components to be loading. Aimed at reusing code for the common pattern of showing a loading image/message when an asynchronous operation is running and a component depends on it.

Install

npm install --save react-hoc-loading

Use

With decorators.

import loading from 'react-hoc-loading';
import React from 'react';

@loading({ LoadingComponent: () => <span>Loading...</span> })
class MyComponent extends React.Component {
  render() {
    return (
      <div>
        {this.renderLoading()}
       </div>
    );
  }
}

<MyComponent loading={false}> // <div></div>
<MyComponent loading> // <div><span>Loading...</span></div>

Set a default LoadingComponent globally with .setDefaultLoadingComponent

loading.setDefaultLoadingComponent(() => <img src="loading.gif" />);

@loading()
class MyComponent extends React.Component { ... }

<MyComponent loading> // <div><img src="loading.gif" /></div>

Use different CSS classes for different components with the className option

loading.setDefaultLoadingComponent(className => <img className={className} src="loading.gif" />);

@loading({ className: "my-class" })
class MyComponent extends React.Component { ... }

<MyComponent loading> // <div><img className="my-class" src="loading.gif" /></div>

Pass props to the LoadingComponent when calling this.renderLoading

loading.setDefaultLoadingComponent((message, className) => (
  <div>
    <div>
      <img className={className} src="loading.gif" />
     </div>
    <p>{message}</p>
  </div>
));

@loading({ className: 'my-class' })
class MyComponent extends React.Component {
  render() {
    return (
      <div>
        { this.renderLoading({ message: 'Fetching data from the server', className: 'my-other-class' }) }
      </div>
    );
  }
}

<MyComponent loading>
// Will render
<div>
  <div>
    <img className="my-other-class" src="loading.gif" />
  </div>
  <p>Fetching data from the server</p>
</div>

Note: The className prop passed to this.renderLoading overrides the className option.

Reference

loading

(options: LoadingOptions) => (Component: React.ComponentType<any>) => React.ComponentType<any>

A function that takes some options and returns a HOC that will extend Component with the renderLoading method.

#renderLoading

(props: object) => React.Element<typeof LoadingComponent>

It is a method of the extended component returned by loading()(). Returns the LoadingComponent element if this.props.loading is true. The className passed to renderLoading will override the one passed in LoadingOptions.

LoadingOptions

type LoadingOptions = {
  LoadingComponent: React.ComponentType<any>?,
  className: string?,
  loadingPropOptional: boolean,
  fullDisplayName: boolean
};

|Option|Default|Description| |-|-|-| |LoadingComponent|Set with loading.setDefaultLoadingComponent|The component that will be rendered when calling renderLoading| |className|undefined|A CSS class that will be passed to the component rendered with renderLoading| |loadingPropOptional|false|Makes the loading property of the resulting Component optional instead of required using PropTypes| |fullDisplayName|false|If true the displayName of the resulting component will be 'Loading(Component)' instead of 'Component'|

loading.setDefaultLoadingComponent

(DefaultComponent: React.ComponentType<any>) => void

Sets the default LoadingComponent option globally. By default it is () => <div>Loading</div>

loading.setDefaultBaseComponent

(DefaultBaseComponent: React.ComponentType<any>) => void

Sets the default component passed to the HOC as its first argument. Be default it is React.PureComponent, but you will always pass your own component to the HOC unless you're doing something funky.