1.0.2 • Published 3 years ago

with-api-hook v1.0.2

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

with-api

Made with create-react-library

NPM JavaScript Style Guide

Install

npm install --save with-api-hook

Usage

import React from 'react'

import { WithApi, STATUS } from 'with-api-hook'
import ApiSerivce from './api.serivce'

const api = new ApiSerivce()

const App = () => {
    // eslint-disable-next-line no-unused-vars
    const [{ data, error, status, isLoading, reset }, fetchSomething] = WithApi(
        api.fakeRequest
    )

    React.useEffect(() => {
        ; (async () => {
            const { status, ...other } = await fetchSomething();
            console.log(STATUS)
            if (status === STATUS.Success) {
                console.log(status, other);
            }
            else if (status === STATUS.Failure) {
                console.log("Error Handler", status, other);
            }
        })()
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [])

    return (
        <>
            <div>
                <h1>Status: {status}</h1>
            </div>
            <div>{isLoading ? <h1>Loading...</h1> : JSON.stringify(data)}</div>
            {
                error ? <div>
                    <h1>Error: {error}</h1>
                </div> : null
            }
        </>
    )
}

export default App

License

MIT © 1997roylee

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago