An open API service indexing awesome lists of open source software.

https://github.com/hacknlove/useendpoint


https://github.com/hacknlove/useendpoint

Last synced: 2 days ago
JSON representation

Awesome Lists containing this project

README

          

# useendpoint
![coverage 75%](https://img.shields.io/badge/coverage-75%25-brightgreen)

## Install
```
npm i @hacknlove/useendpoint
```

## Example
```javascript
import React from 'react'
import ReactDOM from 'react-dom'
import useendpoint from '@hacknlove/useendpoint'

function Example () {
const [user, refresh, error] = useendpoint('https://jsonplaceholder.typicode.com/users/1', {}, 5000)

if (!user || !user.name) {
return null
}
return (

Hello {user.name}


)
}

ReactDOM.render(
,
document.querySelector('#root')
)
```

## API

### `useendpoint(fetchOptions, first, interval)`

#### Parameters
* `fetchOptions` are the parameters that you want to pass to fetch. It could be just `url`, or an array with `[url, options]`
* `first` the initial value, until fetch is completed.
* `interval` miliseconds between fetchs

#### Return
Return `[value, refresh, error]`

* `value` The current value returned by fetch
* `refresh` function that force the fetch
* `error` The error returned by fetch, in that case.

## test this module

```
git clone https://github.com/hacknlove/useendpoint
cd useendpoint
npm install
npm test
```