https://github.com/hacknlove/useendpoint
https://github.com/hacknlove/useendpoint
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/hacknlove/useendpoint
- Owner: hacknlove
- License: isc
- Created: 2019-08-01T11:04:54.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T07:25:39.000Z (over 3 years ago)
- Last Synced: 2025-10-13T19:24:41.049Z (8 months ago)
- Language: JavaScript
- Size: 588 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# useendpoint

## 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
```