use-get-request-hook

1.0.9 • Public • Published

UseGetRequestHook

##Install

import useGetRequestHook from 'use-get-request-hook'

useGetRequestHook uses Axios behind the scenes to make an HTTP get request and returns data, status, loading, error. The biggest benefit of this hook is the ability to delay the loading screen.

The hook takes an options object.

  • url: string - endpoint of url to call
  • params: array of strings or numbers - that will be added onto the url
  • delay: number - how long do you want to wait before showing the loading indicator. The default is 500.

options = {
url: string ('https://jsonplaceholder.typicode.com/posts'),
params?: string[] | number[]
delay?: number in ms
}

For Example:

  const App = ()=>{
      const [count, setCount] = useState(1)
      const [options, setOptions] = useState({
        url: 'https://jsonplaceholder.typicode.com/posts',
        params: [count]
      })
    
    const [ data, status, loading, error ] = useGetRequestHook(options)
    return (
        <div>
        // if error
        { error  && <div> show error message </div> }
        { loading
        // loading is longer than delay
        ? (<div> loading ...</div>)
        // data is read to map through or use
        : ({data})
        }
        </div>
    )
  }

Package Sidebar

Install

npm i use-get-request-hook

Weekly Downloads

1

Version

1.0.9

License

MIT

Unpacked Size

14.4 kB

Total Files

10

Last publish

Collaborators

  • atwilliams88