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

hoc-react-loader

v6.3.0

Published

Higher order component to call a load function from props at mount.

Downloads

702

Readme

hoc-react-loader

CircleCI NPM Version Coverage Status

This is a higher order component ("HOC"). It's an advanced pattern used in React that let you reuse code logic, it can be summarized as a component factory. It improves isolation, interoperability and maintainability of your code base.

hoc-react-loader's purpose is to call a load callback passed through the props of a component only once (at componentWillMount). This is convenient to load data from a backend for instance. The component shows a loading indicator when it's waiting for the props to be defined. The loading indicator can be changed easily.

Demos

You can test some examples here.

Installation

npm i --save tinycolor2 hoc-react-loader

tinycolor2 is a peer dependency of hoc-react-loader. It handles color picking for the default loading indicator. You don't have to install it if you use your own loading indicator.

Usage

With this.props

import loader from 'hoc-react-loader'

const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>

export default loader({ print: ['data'] })(Component)

In this case, the loader waits for this.props.data to be truthy, then mounts its child component and calls this.props.load if it exists. This is useful when the parent has control over the injected data, or when the Component is connected with redux. this.props.load should be injected by the parent component or injected by a Container (redux).

The print parameter should be an array of props to waits. All these props should become truthy at some point.

Since the LoadingIndicator is not specified, the default LoadingIndicator is displayed while waiting for all the props. Here's an exemple with a specified loader:

import loader from 'hoc-react-loader'

const MyLoadingIndicator = () => <div>Waiting...</div>
const Component = ({ data }) => <div>Component {data}</div>

export default loader({ print: ['data'], LoadingIndicator: MyLoadingIndicator })(Component)

The print parameter can also be a Promise. The loading indicator is displayed until print Promise is resolved or rejected.

Don't wait

import loader from 'hoc-react-loader'

const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>

export default loader()(Component)

In this example, the loader component doesn't wait for props. this.props.load is called once, but the LoadingIndicator component isn't displayed.

Load as a function parameter

import loader from 'hoc-react-loader'

const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>

export default loader({ load: () => console.log('here') })(Component)

In this case, the loader calls this.props.load if it exists AND the load parameter, resulting in here to be printed.

The default print parameter value is true. It means that in this example the LoadingIndicator isn't displayed.

Load as a string parameter

import loader from 'hoc-react-loader'

const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>

export default loader({ load: 'myLoader' })(Component)

In this case, the loader calls this.props.myLoader if it exists.

The default print parameter value is true. It means that in this example the LoadingIndicator isn't displayed.

Print as a function

The print parameter can also be a function. Then the props and context are given to it (in this order), and it should return a boolean indicating wether or not the actual component should be displayed.

Error handling

The error parameter allows to specify a prop that indicates wether or not a placeholder error component should be displayed in replacement of the real component. It's usefull when data that are required for the correct display of a component are missing.

Like for the print prop, error can be a boolean, a string (referencing a prop name), an array of string (an array of prop names) or a function (whose result will be converted to boolean).

// default error component will be displayed if 'error' prop is truthy
export default loader()(MyComponent)

// default error component will be displayed
export default loader({ error: true })(MyComponent)

// default error component will be displayed if 'errorMessage' prop is truthy
export default loader({ error: 'errorMessage' })(MyComponent)

// CustomErrorComponent will be displayed if 'error' prop is truthy
export default loader({ ErrorIndicator: CustomErrorComponent })(MyComponent)

Delay parameter

When a component loads very quickly, you will see a flash of the loading component. To avoid this behaviour, you can add a delay parameter to the loader with a time in milliseconds. Then, the loading indicator will be rendered after the delay if the Component can't be rendered before that.

// loading indicator will be displayed only after 200ms
export default loader({ print: ['data'], delay: 200 })(MyComponent)

By default, no delay is defined.

About alakarte

alakarte is created by two passionate french developers.

Do you want to contact them ? Go to their website

Zenika

This library with initiated thanks to Zenika.