0.1.4-beta • Published 3 years ago

hook-essentials v0.1.4-beta

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

hook-essentials (Beta)

A Collection of excellent react hooks.

  • useAsyncCallback
  • useStateObject
  • useFormState
  • useSelection
  • useThrottle
  • useToggle
  • useGetParams (peer-dependency: react-router-dom)

useAsyncCallback


Example Usage:

import { useAsyncCallback } from 'hook-essentials';
import api from './apiClient';

function App() {
    const {
        result,
        loading,
        error,
        callback: doAction,
        reset
    } = useAsyncCallback(api.doAction, {
        defaultResult: {
            message: 'no results yet'
        }
    });

    if (error) {
        return (
            <h2>There was an Error.</h2>
        );
    }

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

    return (
        <div>
            <Button onClick={doAction}>Do Action</Button>
            <Button onClick={reset}>reset</Button>
            <pre>{JSON.stringify(result, null, 4)}</pre>
        </div>
    );
}

Signature:

(alias) useAsyncCallback(
    callback,
    {
        defaultResult = undefined,
        autoCall = false,
        autoCallArgs = [],
    } = {}
) : ({
    result: mixed
    loading: boolean
    error: false|Error|mixed
    callback: function,
    reset: function
})

Coverage

File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files96.4595.3595.5996.77
testUtils.js100100100100
useAsyncCallback.js100100100100
useFormState.js10095.8310010013
useGetParams.js100100100100
useSelection.js100100100100
useStateObject.js84.448081.2585.719,28-30,42,59
useThrottle.js100100100100
useToggle.js100100100100
0.1.4-beta

3 years ago

0.1.3-beta

3 years ago

0.1.2-beta

3 years ago

0.1.1-beta

3 years ago

0.1.0-beta

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago