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-use-xhr

v0.0.1

Published

A simple React hook that communicates via XMLHttpRequest(XHR)

Downloads

346

Readme

react-use-xhr

CircleCI

A simple React hook that communicates via XMLHttpRequest(XHR)

:eyes: Overview

import {useXhr} from 'react-use-xhr'

const YourComponent = () => {
  const result = useXhr({
    httpMethod: 'GET',
    url: 'https://your-awesome-api.com',
  })
  console.log(result)
  return <YourSubComponents />
}

:point_down: Example of console.log output.

Object {isLoading: true, events: Array[0]}
Object {isLoading: true, events: Array[1]}
Object {isLoading: true, events: Array[2]}
Object {isLoading: true, events: Array[3]}
Object {isLoading: false, events: Array[4], xhr: XMLHttpRequest}
Object {isLoading: false, events: Array[4], xhr: XMLHttpRequest}

:cat: Features

  • Communicates using XMLHttpRequest object.
    • It also returns raw XMLHttpRequest results.
  • Works on IE11(Internet Explorer 11).
    • For example, it does not depend on the Promise object.

NOTE: Why use XMLHttpRequest?

  • Because of my work, it's difficult to polyfill fetch.
  • If you can use fetch, I think there are few reasons to use XHR.

:rocket: Installation

npm install react-use-xhr

:book: Usage

useXhr API

Overview

const result = useXhr(query, queryId, options)

Parameters

  • query: An object for building HTTP request.
    • httpMethod: One of "GET", "POST", "PUT", "PATCH" and "DELETE".
    • url: An URL string such as "https://example.com" and "/foo". It is passed as an argument of open.
    • headers (Optional): An object such as {"Content-Type": "application/json"}. Each key/value is passed as an argument of setRequestHeader.
  • queryId (Optional): It is used when you want to send a request multiple times with the same query value. It can receive a string, a number or an undefined. If undefined, then query is used for this value.
    • Default value is undefined.
  • options (Optional): An object such as {maxResultCache: 100, timeout: 5000}.
    • Default value is {}.
    • maxResultCache (Optional): The number of responses to cache.
      • Default value is 1.
    • timeout (Optional): Milliseconds until the communication is terminated. It is passed as an argument of timeout.
      • Default value is undefined.

Return value

An object that contains the communication status and the response result.

  • isLoading: A flag indicating whether communication is in progress.
  • events: An array that stores the occured XHR events in order.
  • xhr (May not exist): An instance of XMLHttpRequest. This will be passed when the communication is complete. It does not always exist when isLoading is true.

:bust_in_silhouette: Examples

Handle communication failures

XHR tells exceptional results to the outside on the event.
There is a method of judging the result depending on the event that occurred.

const isCommunicationFailed = (events) => {
  return events.some(event => ['abort', 'error', 'timeout'].indexOf(event.type) !== -1)
}
const result = useXhr(query)
if (isCommunicationFailed(result.events)) {
  // Do stuff.
}

Send the same query multiple times

If you specify and change queryId, it will send the request again even if query is the same.

// When discarding the request every 1 minute.
const everyOneMunite = Math.floor(new Date().getTime() / (1000 * 60))
const result = useXhr(query, everyOneMunite)

Cache many request results

useXhr first searches the cache for a result with the same queryId.
Since the initial value is 1, only the most recent result is saved, but increasing this value will save many results.

const result = useXhr(query, undefined, {maxResultCache: 9999})