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-network-state-hook

v0.3.1

Published

A react machine state hook to handle your network requests.

Downloads

2

Readme

React Network State Hook

A react machine state hook to handle your network requests.

Activity

License Coverage GitHub issues GitHub all releases GitHub Workflow Status contributors npm

data

Data that returns the network call.

Generic

Data could be defined with a generic when we define our hook to be used:

 const { data, meta, signal, actions } = useNetworkState<string>();

meta

loading

The hook will return a loading state.

error

The hook will return an error state.

errorMessage

The hook will return an error message.

Actions

start

Turns the loading state to true.

end

Turns the loading state to false.

abort

Turns the loading state to false and abort the network call.

resetError

Turns the error state to false and initialize error message to ''.

setError

Turns the loading state to false, the error state to true and sets the error message.

setData

Set data that should return the hook.

setLoading

Set the loading state.

resetSignal

Reset the abort controller to a new one.

This action is important when you want to abort the network call.

actions.start();

try {
  const response = await fetch(url, { signal }) as any;
  const d = await response.json();

  actions.setData(d);
} catch (error: any) {
  if (error as DOMException) {
    actions.resetSignal();
    return;
  }

  actions.setError();
} finally {
  actions.end();
}

setController

Set the abort controller.

Use example

useFetchFromMyApi.hook.ts

import { useNetworkState, UseNetworkStateReturn } from 'useNetworkSate';

type UseFetchFromMyApiReturn = {
  data: unknown;
  meta: {
    isLoading: boolean;
    isError: boolean;
    errorMessage: string;
  };
  fetchFromMyApiExampleByName: (name: string) => UseNetworkStateReturn;
};

export default function useFetchFromMyApi(): UseFetchFromMyApiReturn {
  const { data, meta, actions, signal } = useNetworkState();

  const fetchFromMyApiExample = async (name = 'defaultName') => {
    actions.startRequest();

    try {
      const response = await fetch(`https://my-api.com/?name=${name}`, signal);
    } catch (error) {
      actions.setErrorState(error);
    } finally {
      actions.endRequest();
    }
  };

  return { data, meta, fetchFromMyApiExampleByName };
}

App.component.ts

import { useEffect } from 'react';
import useFetchFromMyApi from './useFetchFromMyApi';

export default function App() {
  const { data, meta, fetchFromMyApiExampleByName } = useFetchFromMyApi();

  useEffect(() => {
    fetchFromMyApiExampleByName('Rick');
  }, []);

  if (meta.isLoading) {
    return <div>Loading...</div>;
  }

  return <div>data: {JSON.stringify(data, null)}</div>;
}