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

@ohm-vision/react-async

v1.0.1

Published

Extentions to React to support asynchronous calls

Downloads

151

Readme

react-async

Wrapper for react to support asynchronous calls

npm version

"Buy Me A Coffee"

Installation

Run the following command

npm install @ohm-vision/react-async

Usage

There are several effects offered depending on your needs

useAsync

This is will likely be your best friend and the method you use the most.

It will fire your async function when a dependency has changed and return a Tuple indicating if the method is still loading

Important note:

This method does not wrap anything in a try-catch block, error management is YOUR responsibility

Example

import { useAsync } from "@ohm-vision/react-async"

export function MyAwesomeComponent(props) {
    const [ loading, result ] = useAsync(async (abortSignal: AbortSignal) => {
        // just in case we don't want to use the `useFetch`
        // ie. we have our own API classes, or are calling a third-party sdk
        const result = await fetch("http://example.com", {
            method: "GET",
            signal: abortSignal
        });

        return await result.json();
    }, [ props.dep1 ], () => {
        console.log("I was unloaded");
    });

    if (loading) {
        // todo: show a skeleton component
        return <>Loading...</>;
    }

    // todo: do something with the result
    // note: `result` will be undefined until the first load is complete
}

useEffectAsync

This is the core async effect with nothing special

Important note:

This method does not wrap anything in a try-catch block, error management is YOUR responsibility

Example

import { useEffectAsync } from "@ohm-vision/react-async"

export function MyAwesomeComponent(props) {
    const loading = useEffectAsync(async (signal: AbortSignal) => {
        // some special call

        // all processing is handled here - you manage everything

        // the AbortSignal will automatically be called when the component is unmounted
    }, [ props.dep1 ], () => {
        console.log("I was unloaded");
    });

}

useFetch

This is an extension to the useAsync which wraps the standard fetch call using what I consider to be a good standard for handling the operation

Used for loading data on the client

Important note:

Unlike other methods, this hook will capture the error response and return it to you as the third tuple (triple/triplet?)

This is done because unlike the other methods, there is less control over the function lifecycle when using this hook.

props (param 1)

This accepts all properties of the native fetch command and adds the following:

  • url: string | URL | RequestInfo - endpoint to fetch
  • responseType: "raw" | "arrayBuffer" | "blob" | "formData" | "text" | "json" - indicates how to process the successful response
    • When raw is used, the original Response object is returned
    • All other responses, will call the appropriate method to read the body and return a mutated response object

Example

import { useFetch } from "@ohm-vision/react-async"

export function MyAwesomeComponent(props) {
    const [ loading, { body }, error ] = useFetch({
        url: "https://example.com",
        responseType: "text",
    }, [ props.dep1 ], () => {
        console.log("I was unloaded");
    });

    if (loading) {
        return <>Loading...</>
    }

    if (error) {
        return <>ERROR: {error.toString()}</>;
    }

    // todo: do something with the body
}

useMemoAsync

This is an extension to the useEffectAsync which attempts to operate similarly to React's native useMemo function

Used for firing asynchronous calls which return a result, could be a good alternative if you want to do something special.

This method will just return the result or the previous value if an async call is still running

Important note:

This method does not wrap anything in a try-catch block, error management is YOUR responsibility

Example

import { useMemoAsync } from "@ohm-vision/react-async"

export function MyAwesomeComponent(props) {
    const result = useMemoAsync(async (abortSignal: AbortSignal) => {
        // just in case we don't want to use the `useFetch`
        // ie. we have our own API classes, or are calling a third-party sdk
        const result = await fetch("http://example.com", {
            method: "GET",
            signal: abortSignal
        });

        return await result.json();
    }, [ props.dep1 ], () => {
        console.log("I was unloaded");
    });

    // todo: do something with the result
    // note: `result` will be undefined until the first load is complete
}

Contact Me

Ohm Vision, Inc