Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
react-promise-tracker
Advanced tools
Simple React Promise tracker Hook/HOC helper to add loading spinner indicators
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
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:
This library implements:
npm install react-promise-tracker --save
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.
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:
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.
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');
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});
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).
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
FAQs
Simple React Promise tracker Hook/HOC helper to add loading spinner indicators
The npm package react-promise-tracker receives a total of 13,689 weekly downloads. As such, react-promise-tracker popularity was classified as popular.
We found that react-promise-tracker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.