Coder Social home page Coder Social logo

andytsing / react-promise-tracker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lemoncode/react-promise-tracker

0.0 1.0 0.0 514 KB

Simple promise tracker React Hoc. https://lemoncode.github.io/react-promise-tracker/

License: MIT License

JavaScript 100.00%

react-promise-tracker's Introduction

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 project site

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 wat 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>
  )
};
  • To add a cool spinner component you can make use of react-spinners:

  • 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

react-promise-tracker's People

Contributors

brauliodiez avatar nasdan avatar fjcalzado avatar asciant avatar jsagardoy avatar gangsthub avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.