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

v1.0.2

Published

react wrapper (HOC) for tracking the status of async requests.

Downloads

18

Readme

react-async-tracker

react wrapper (HOC) for tracking the status of async requests.

NPM JavaScript Style Guide

A package that reuses state logic responsible for tracking an async request's status. Supports promises, thunks, and any other type of async request. Don't forget to ⭐️ if you like.

Install

npm install react-async-tracker

or if you're using yarn

yarn add react-async-tracker

Usage

This package reuses state logic responsible for tracking an async request's status. It exports a wrapper function, and an object that contains the possible status values which you can use to compare your request's status against. The values that a request status can take are as follows:

  • undefined: Request not yet initiated
  • FETCH_STATUS.ACTIVE: 🏋
  • FETCH_STATUS.SUCCESS: 😇
  • FETCH_STATUS.FAIL: 😢
  • FETCH_STATUS.INACTIVE: the request is set to this value ONLY after finishing (either with success or failure). BTW, this is equal to 0, which means it's a falsy value.
import { escortAsync, FETCH_STATUS } from 'react-async-tracker';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
    };
    this.fetchData = this.fetchData.bind(this);
  }

  componentWillUnmount() {
    this.props.escort.cancelRequests();
  }

  fetchData() {
    this.props.escort.makeRequest(
      'myRequest',
      dispatch(thunkReturningFunction())
    )
    .then((data) => this.setState({ data }))
    .catch((e) => console.log('e', e));
  }

  render() {
    const { escort } = this.props;
    const { data } = this.state;
    return (
      <div>
          {data && data.length ? (
            <ul>
              display your fetched data
            </ul>
          ) : (
            <Fragment>
              {/* Create a reusable component that replicates the behavior below */}
              <span>{!escort.fetchStatus('myRequest') && (
                <button onClick={this.fetchData}>Fetch Data</button>
              )}
              </span>

              <span>{escort.fetchStatus('myRequest') === FETCH_STATUS.ACTIVE && (
                LOADING...
              )}</span>

              <span>{escort.fetchStatus('myRequest') === FETCH_STATUS.SUCCESS && (
                <p>😃</p>
              )}</span>

              <span>{escort.fetchStatus('myRequest') === FETCH_STATUS.FAIL && (
                <p>😔</p>
              )}</span>
            </Fragment>
          )}
        </h3>
      </div>
    )
  }
}

export default escortAsync(MyComponent);

The escortAsync function is a wrapper which passes down the object escort to your props. Use this object to make requests, track their status, and cancel them:

  • escort.makeRequest(myRequestName, request): makes the request. Make sure whatever promise-chaining functions you use (then, catch, finally) are chained to it and NOT the request in the arguments!
  • escort.fetchStatus(myRequestName): returns the status of the request. Compare this against one of the values of the FETCH_STATUS enum (scared by the word enum? pretend it's not there) object.
  • escort.cancelRequests(): use this in the componentWillUnmount method to cancel all ongoing requests and avoid the memory leak console warning. And no, you can't cancel just one request.

Escort.makeRequest() Options:

PS: it's very important we don't rely on FETCH_STATUS.SUCCESS as a condition to access whether we have data. Why?

  1. FETCH_STATUS.SUCCESS is temporary. The value should eventually change to FETCH_STATUS.INACTIVE (unless the user chooses not to for that individual request; that is).
  2. fetchStatus(REQUEST_NAME) becomes assigned to SUCCESS before the result of the promise is resolved. Some time is given before changing value to INACTIVE so that users get to see the SUCCESS result on their screens.
  3. Assuming you persist the data, e.g. store it in redux, unmounting the component will reset the status of the request to undefined while the data is still available.

License

MIT © @tareqdayya