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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@anb98/react-hooks

v1.3.2

Published

Custom hooks for react

Readme

Hooks

Table of Contents

Installation

npm i @anb98/react-hooks

setGlobals

This function allows you to set globals options for useFetch, useFetchData, useLazyFetch and useLazyFetchCache

Usage

import { setGlobals } from '@anb98/react-hooks';

setGlobals({
    baseURL: 'http://your-base-url',
    headers: {},
    withCredentials: true,
})

*You don't need to set / at the end of baseURL

setGlobals options

|Property|Description|Type|Default| |-|-|-|-| | baseURL | It sets base url to be concatenate with url of useDataApi options | string | '' | | headers | Request headers | any | null | | withCredentials | Enable cookie credentials | boolean | false |

useLazyFetch

This hook is a wrapper for usePromise hook which consumes an API when calling the handler function.

Usage

import { useLazyFetch } from '@anb98/react-hooks';

const options = {
    url: 'http://your-endpoint-url',
    initialData: {},
    request: { headers: { example: 'test'} },
    onCancel: () => {},
    onComplete: (data, err) => {},
    onFail: (err) => {},
    onSuccess: (data) => {},
};

const TestComponent = () => {
    const [{ 
        data,
        error,
        isError,
        isLoading,
        isSuccess,
        status,
    }, fetchData, resetState, cancelFetch ] = useLazyFetch(options);
    
    const getData = () => {
        const fetchDataOptions = {
            body:{},
            headers: {},
            method: 'post',
            params: {},
            url: '',
            withCredentials: true
        }

        fetchData(fetchDataOptions);
    };

    return (<button onClick={getData}>test</button>);
};

export default TestComponent;

Initial options

|Property|Description|Type|Default| |-|-|-|-| | url | Initial URL to request | string | globals.baseURL | | initialData | Initial data to return as result | any | null | | request | Request config object axios | Request config axios | {} | | onFail | Callback called when request fails | function(err)| ()=>{} | | onSuccess | Callback called when request fails | function(data) | ()=>{} | | onComplete | Callback called when request completes | function(data, err) | ()=>{} | | onCancel | Callback called when request cancels | function() | ()=>{} |

Returned state

|Property|Description|Type|Default| |-|-|-|-| | data | Result of the request | any | initialData | | error | Error of the request | any | null | | isError | It shows if the request fails | boolean | false | | isLoading | It shows if the request is loading | boolean | false | | isSuccess | It shows if the request completed successfully | boolean | false | | status | It shows the request's status | idle | pending | resolved | rejected | idle |

resetState function will reset returned state to initial state.

fetchDataOptions options uses type Request config from axios

useFetch

This hook consumes API when component is mounted and also when calling the handler function.

By default it request with GET method unless you change initial options.

This hook is a wrapper for useLazyFetch.

Usage

import { useFetch } from '@anb98/react-hooks';

const options = {
    deps: [],
    initialData: {},
    request: { headers: { example: 'test'} },
    onCancel: () => {},
    onComplete: (data, err) => {},
    onFail: (err) => {},
    onSuccess: (data) => {},
};

const TestComponent = () => {
    const [{ 
        data,
        error,
        isError,
        isLoading,
        isSuccess,
        status,
    }, fetchData, resetState, cancelFetch ] = useFetch('http://your-endpoint-url', options);
    
    const getData = () => {
        const fetchDataOptions = {
            body:{},
            headers: {},
            method: 'post',
            params: {},
            url: '',
            withCredentials: true
        }

        fetchData(fetchDataOptions);
    };

    return (<button onClick={getData}>test</button>);
};

export default TestComponent;

URL first param is mandatory

Initial options

|Property|Description|Type|Default| |-|-|-|-| |deps| Dependency list to run hook | Array<any> | [] |

usePromise

This hook executes a Promise when calling the handler function.

Usage

import { usePromise } from '@anb98/react-hooks';

const promise = (example, test) => Promise.resolve({ example, test });

const options = {
    deps:[],
    params: ['example', 'test'],
    initialData: {},
    onComplete: (data, err) => {},
    onFail: (err) => {},
    onSuccess: (data) => {},
    onUnmount: ()=>{},
};

const TestComponent = () => {
    const [{ 
        data,
        error,
        isError,
        isLoading,
        isSuccess,
        status,
    }, promiseHandler, resetState ] = usePromise(promise, options);
    
    const getPromiseResult = () => {
        promiseHandler('example', 'test');
    };

    return (<button onClick={getPromiseResult}>test</button>);
};

export default TestComponent;

promise function param is mandatory

handlerOptions are passed to promise function; otherwise it passes params from initial options

resetState function will reset returned state to initial state.

Initial options

|Property|Description|Type|Default| |-|-|-|-| |deps| Dependency list to run hook | Array<any> | [] | |params| Default params to use in promise handler | any[] | [] | | initialData | Initial data to return as result | any | null | | onFail | Callback called when promise fails | function(err)| ()=>{} | | onSuccess | Callback called when promise success | function(data) | ()=>{} | | onComplete | Callback called when promise completes | function(data, err) | ()=>{} | | onUnmount | Callback called when hook is unmounted | function() | ()=>{} |

Returned state

|Property|Description|Type|Default| |-|-|-|-| | data | Result of the request | any | initialData | | error | Error of the request | any | null | | isError | It shows if the request fails | boolean | false | | isLoading | It shows if the request is loading | boolean | false | | isSuccess | It shows if the request completed successfully | boolean | false | | status | It shows the request's status | idle | pending | resolved | rejected | idle |

useFetchCache and useLazyFetchCache

This hooks allow to cache results from request previously fetched. To use this hooks you first need CacheProvider.

import { CacheProvider } from '@anb98/react-hooks';

const App = () => (
    <div className="App">
        <CacheProvider>
            <Main/>
        </CacheProvider>
    </div>
);

All the options and returned values for useFetchCache and useLazyFetchCache are the same from useFetch and useLazyFetch respectively.

To refresh the next request you can add { refresh: true } as second param when calling handler.

Example:

<button onClick={()=>handler({}, { refresh: true })}>
    Fetch Cache
</button>

useSearch

This hook filters results when searching.

Usage

import { useSearch } from '@anb98/react-hooks';

const options = {
    allowFields: [],
    denyFields: []
    sourceData: []
};

const TestComponent = () => {
    const {
        setSourceData,
        setSearchValue,
        filtered,
        sourceData
    } = useSearch(options);

    return (
        <div>
            <input type="text" onChange={(evt)=> setSearchValue(evt.target.value) } />

            <ul>
                {
                    filtered.map((el)=>(
                        <li key={el.id}>{JSON.stringify(el)}</li>
                    ))
                }
            </ul>
        </div>
    );
}

Initial options

|Property|Description|Type|Default| |-|-|-|-| | allowFields | Fields in which search | Array<string> | [] | | denyFields | Fields in which not search | Array<string> | [] | | sourceData | Source data to search | Array<Object> | [] |

Returned

|Property|Description|Type|Default| |-|-|-|-| | setSourceData | Function to set sourceData | function(Array<Object>) | | | setSearchValue | Function to set search value | function(string) | | | filtered | Filtered results from sourceData | Array<string> | [] | | sourceData | Previously set source data | Array<Object> | [] |