Ecosyste.ms: Awesome

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

https://github.com/smmoosavi/react-use-data-loader

React hook for loading data
https://github.com/smmoosavi/react-use-data-loader

load-data react react-hooks

Last synced: 3 months ago
JSON representation

React hook for loading data

Lists

README

        

# react-use-data-loader

React hook for loading data

# Installation

Using npm:

```
$ npm install --save react-use-data-loader
```

Using yarn:

```
$ yarn add react-use-data-loader
```

Since this module uses React's upcoming Hooks feature, to try this out you'll need to install the 16.7.0-alpha.0 version of react and react-dom:

```
$ yarn add [email protected] [email protected]
```

# Usage

```jsx
import React from 'react'
import { useDataLoader } from 'react-use-data-loader'

async function getData(id) {
/* async api call */

return 'item ' + id
}

function Example({ id }) {
const { data, error, loading, retry } = useDataLoader(getData, id)

if (loading) {
return

loading...

}

if (error) {
return

Error

}

return
}

function App() {
return
}
```

# API

```ts

useDataLoader(getData: GetData, ...args: Args) => ({
data: Data | null,
error: Error | null,
loading: boolean,
retry: Retry,
})

type GetData = (...args: Args) => Promise
type Retry = () => void

```