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-promise-tracker

v2.1.1

Published

Simple React Promise tracker Hook/HOC helper to add loading spinner indicators

Downloads

60,444

Readme

Build Status

react-promise-tracker

Simple promise tracker React Hoc. You can see it in action in this Live Demo, and find the basic info to get started in this post.

For detailed information check out the documentation

Why do I need this?

Sometimes we need to track blocking promises (e.g. fetch or axios http calls), and control whether to display a loading spinner indicator not, you have to take care of scenarios like:

  • You could need to track several ajax calls being performed in parallel.
  • Some of them you want to be tracked some others to be executed silently in background.
  • You may want to have several spinners blocking only certain areas of the screen.
  • For high speed connection you may want to show the loading spinner after an small delay of time to avoid having a flickering effect in your screen.

This library implements:

  • A simple function that will allow a promise to be tracked.
  • A Hook + HOC component that will allow us wrap a loading spinner (it will be displayed when the number of tracked request are greater than zero, and hidden when not).

Installation

npm install react-promise-tracker --save

Usage

Whenever you want a promise to be tracked, just wrap it like in the code below:

+ import { trackPromise} from 'react-promise-tracker';
//...

+ trackPromise(
    fetchUsers(); // You function that returns a promise
+ );

Then you only need to create a spinner component and make use of the usePromiseTracker, this hook will expose a boolean property that will let us decide whether to show or hide the loading spinner.

Basic sample:

import React, { Component } from 'react';
+ import { usePromiseTracker } from "react-promise-tracker";

export const LoadingSpinerComponent = (props) => {
+ const { promiseInProgress } = usePromiseTracker();

  return (
    <div>
    {
+      (promiseInProgress === true) ?
        <h3>Hey I'm a spinner loader wannabe !!!</h3>
      :
        null
    }
  </div>
  )
};
  • Then in your application entry point (main / app / ...) just add this loading spinner to be displayed:
import React from 'react';
+ import { LoadingSpinnerComponent} from './loadingSpinner';

export const AppComponent = (props) => (
  <div>
    <h1>Hello App!</h1>
+   <LoadingSpinnerComponent />
  </div>
);

Sample with areas:

Using react-promise-tracker as is will just display a single spinner in your page, there are cases where you want to display a given spinner only blocking certain area of the screen (e.g.: a product list app with a shopping cart section. We would like to block the ui (show spinner) while is loading the product, but not the rest of the user interface, and the same thing with the shopping cart pop-up section.

Shopping cart sample

The promiseTracker hooks exposes a config parameter, here we can define the area that we want to setup (by default o area). We could just feed the area in the props of the common spinner we have created

export const Spinner = (props) => {
+  const { promiseInProgress } = usePromiseTracker({area: props.area});

  return (
    promiseInProgress && (
      <div className="spinner">
        <Loader type="ThreeDots" color="#2BAD60" height="100" width="100" />
      </div>
    )
  );
};

We could add the default-area to show product list spinner (no params means just default area):

import React from 'react';
+ import { LoadingSpinnerComponent} from './loadingSpinner';

export const ProductListComponent = (props) => (
  <div>
    ...
+   <LoadingSpinnerComponent /> // default-area
  </div>
);

And we add the shopping-cart-area to show shopping cart spinner:

import React from 'react';
+ import { LoadingSpinnerComponent} from './loadingSpinner';

export const ShoppingCartModal = (props) => (
  <div>
+   <LoadingSpinnerComponent area="shopping-cart-area" />
  </div>
);

The when we track a given promise we can choose the area that would be impacted.

+ import { trackPromise} from 'react-promise-tracker';
...
+ trackPromise(
    fetchSelectedProducts();
+ ,'shopping-cart-area');

Sample with delay:

You can add as well a delay to display the spinner, When is this useful? if your users are connected on high speed connections it would be worth to show the spinner right after 500 Ms (checking that the ajax request hasn't been completed), this will avoid having undesired screen flickering on high speed connection scenarios.

export const Spinner = (props) => {
+  const { promiseInProgress } = usePromiseTracker({delay: 500});

Demos

Full examples:

  • 00 Basic Example: minimum sample to get started.

  • 01 Example Areas: defining more than one spinner to be displayed in separate screen areas.

  • 02 Example Delay: displaying the spinner after some miliseconds delay (useful when your users havbe high speed connections).

  • 03 Example Hoc: using legacy high order component approach (useful if your spinner is a class based component)

  • 04 Initial load: launching ajax request just on application startup before the spinner is being mounted.

  • 05 Typescript: full sample using typescript (using library embedded typings).

  • 06 Suspense Like: sample implementing a suspense-like component (typescript).

  • 07 Suspense Custom: sample implementing a suspense-like component that can be customized by passing a spinner component of your choice (typescript).

About Basefactor + Lemoncode

We are an innovating team of Javascript experts, passionate about turning your ideas into robust products.

Basefactor, consultancy by Lemoncode provides consultancy and coaching services.

Lemoncode provides training services.

For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend