react-use-data-loader
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

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 react@16.7.0-alpha.0 react-dom@16.7.0-alpha.0

Usage

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 <div>loading...</div>
  }
 
  if (error) {
    return <div>Error</div>
  }
 
  return <ViewData data={data} />
}
 
function App() {
  return <Example id={3} />
}

API

 
useDataLoader(getDataGetData, ...argsArgs) => ({
  data: Data | null,
  error: Error | null,
  loading: boolean,
  retry: Retry,
})
 
type GetData = (...args: Args) => Promise<Data>
type Retry = () => void
 

Package Sidebar

Install

npm i react-use-data-loader

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

8.67 kB

Total Files

6

Last publish

Collaborators

  • smmoosavi