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

global-state-axios-hooks

v1.0.2

Published

`GlobalState` is a class for managing the global state of an application. `globalStateHooks` is a set of functions and React hooks that allow you to manage global state, handle promises, and make `axios` requests within React components.

Downloads

11

Readme

GlobalState and Axios Hooks

GlobalState is a class for managing the global state of an application. globalStateHooks is a set of functions and React hooks that allow you to manage global state, handle promises, and make axios requests within React components.

Description

GlobalState

GlobalState allows you to store some state and subscribe to changes in it.

Key Methods

  • constructor(state?: T): Initializes the global state with an initial value state.
  • next(state?: T): void: Sets a new state value and notifies all subscribers of the change.
  • subscribe(subscriber: GlobalStateSubscriber<T>): GlobalStateUnsubscribe: Adds a subscriber for state changes. Returns a function to unsubscribe.
  • getValue(): T | undefined: Returns the current state value.

Hooks for Using with GlobalState

These hooks allow you to use GlobalState in React components.

Key Hooks and Functions

  • getGlobalStateHook<T>(globalState: GlobalState<T>): () => T | undefined: Creates a React hook to subscribe to changes in the global state.
  • getPromiseStateLoadingHook(globalState: GlobalState<PromiseState<any>>): () => boolean: Returns a hook to get the loading state (true/false).
  • getPromiseStateDataHook<T>(globalState: GlobalState<PromiseState<T>>): () => T | undefined: Returns a hook to get data from PromiseState.
  • getPromiseStateErrorHook(globalState: GlobalState<PromiseState<any>>): () => Error | undefined: Returns a hook to get errors from PromiseState.
  • promiseToGlobalState<T>(promise: Promise<T>, globalState: GlobalState<PromiseState<T>>): Promise<T>: Handles a promise and stores its state in GlobalState.
  • axiosResponseToGlobalState<T>(promise: Promise<AxiosResponse<T>>, subject: GlobalState<PromiseState<T>>): Promise<T>: Handles an axios response and stores its state in GlobalState.
  • getRequestHooks<T, K>(axiosCall: (args?: K) => Promise<AxiosResponse<T>>): RequestHooks<T, K>: Creates a set of hook functions for making axios requests and managing loading, data, and error states.
  • getGlobalStateHookSetterGetter<T>(globalState: GlobalState<T>): [() => T | undefined, (value?: T) => void, () => T | undefined]: Creates a set of functions: hook, setter and getter for managing global state.

Example Usage

Using GlobalState with Axios in React

import React, { useEffect } from 'react';
import { getRequestHooks } from 'global-state-axios-hooks';
import axios from 'axios';

// Define the request hook
export const [loadSomething, useSomethingLoading, useSomethingData, useSomethingError] =
  getRequestHooks(() => axios({ url: '/api/something', method: 'get' }));

// Using the hook in a component
const MyComponent = () => {
  const loading = useSomethingLoading();
  const data = useSomethingData();
  const error = useSomethingError();

  useEffect(() => {
    loadSomething();
  }, []);

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

  if (error) {
    return <div>Error: {error.message}</div>;
  }

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

Using GlobalState in React

import React from 'react';
import { GlobalState, getGlobalStateHookSetterGetter } from 'global-state-axios-hooks';

// Create GlobalState
const counterState = new GlobalState<number>(0);

// Define hook, setter and getter for counterState.
export const [useCounter, setCounter, getCounter] = getGlobalStateHookSetterGetter(counterState);

// Using the hook and functions in a component
const MyComponent = () => {
  const counter = useCounter();

  return (
    <div>
      <div>Counter: {counter}</div>
      <button type="button" onClick={() => setCounter(getCounter() + 1)}>Add 1</button>
    </div>
  );
};