async-data-access
React component for asynchronously fetching data
Install
npm install --save async-data-access
Usage
This component will load data asynchronously using the provided fetch
function, and pass through status values and payload via the render-props pattern to a provided child render method. An optional transform
function can be given to transform the fetched response into a different format.
AsyncDataAccess is agnostic about how data is fetched, with only the expectation that the fetching will be performed asynchronously. This means that it can be written using a modern async
function, or alternatively using a Promise
. The fetch method can potentially make multiple API calls and gather their results together.
Props accepted by this component are:
{
fetch: PropTypes.func.isRequired,
transform: PropTypes.func,
onError: PropTypes.func,
}
Props passed through to render child, with their types are:
{
isFetching: PropTypes.bool,
didInvalidate: PropTypes.bool,
lastFetchFailed: PropTypes.bool,
lastError: PropTypes.string,
payload: PropTypes.any,
reload: PropTypes.func
}
Example
import React, { Component } from 'react'
import AsyncDataAccess from 'async-data-access'
class Example extends Component {
fetcher = async () => {
return await fetch('http://example.com/movies.json')
}
transformer (response) {
return response.movieList
}
render () {
return (
<AsyncDataAccess fetch={this.fetcher} transform={this.transformer}>
{
props => {
const { isFetching, payload } = props;
const movies = payload || [];
return <>
{ isFetching && <h2>Loading data</h2> }
{
movies.map(
({ title, description }) => <>
<h3>{title}</h3>
<div>{description}</div>
</>
)
}
</>
}
}
</AsyncDataAccess>
)
}
}
License
MIT © stevesims