Simple promise tracker React Hoc. You can see it in action: Live Demo
Sometimes we need to track blocking promises (e.g. fetch http calls), to choose between displaying a loading spinner or not.
This library implements:
- A simple function that will allow a promise to be tracked.
- An 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).
npm install react-promise-tracker --saveWhenever 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 component that will defined a property called trackedPromiseInProgress
And wrap it around the promiseTrackerHoc
import React, { Component } from 'react';
import PropTypes from 'prop-types';
+ import { promiseTrackerHoc} from 'react-promise-tracker';
const InnerLoadingSpinerComponent = (props) => {
return (
<div>
{
(props.trackedPromiseInProgress === true) ?
<h3>Hey I'm a spinner loader wannabe !!!</h3>
:
null
}
</div>
)
};
InnerLoadingSpinerComponent.propTypes = {
trackedPromiseInProgress : PropTypes.bool.isRequired,
};
+ export const LoadingSpinnerComponent = promiseTrackerHoc(InnerLoadingSpinerComponent);-
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>
);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.
We could add the default-area to show product list spinner:
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>
);With this approach, we don't need to define different spinners components, it's only one but it will render when we want to track the desired area:
+ import { trackPromise} from 'react-promise-tracker';
...
+ trackPromise(
fetchSelectedProducts();
+ ,'shopping-cart-area');If you want to see it in action:
We are a team of long-term experienced freelance developers, established as a group in 2010. We specialize in Front End technologies and .NET. Click here to get more info about us.
For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend