https://github.com/lucasconstantino/react-data-loader
Dead simple data loader helper for React
https://github.com/lucasconstantino/react-data-loader
data higher-order-component loader react
Last synced: 6 months ago
JSON representation
Dead simple data loader helper for React
- Host: GitHub
- URL: https://github.com/lucasconstantino/react-data-loader
- Owner: lucasconstantino
- License: mit
- Created: 2017-06-28T04:00:51.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-16T16:53:14.000Z (almost 7 years ago)
- Last Synced: 2025-06-17T10:17:28.121Z (8 months ago)
- Topics: data, higher-order-component, loader, react
- Size: 129 KB
- Stars: 22
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# React Data Loader [](https://travis-ci.org/lucasconstantino/react-data-loader)
> Dead simple data loader helper for React
## Disclaimer
Use it not for big projects! If you project needs lots of API communication you'll be probably better off using tools such as [GraphQL](http://graphql.org/) (I personally recommend [Apollo](https://www.apollodata.com/) for that task) or even [Redux](http://redux.js.org/) with [`redux-thunk`](https://github.com/gaearon/redux-thunk). Sometimes, though, these are too much.
## Why this package?
When loading external data in a React component - be it on user interaction or during mounting phase - we end up rewriting the same logic all over again: loading status, error responses, etc. This could be much simpler and a lot more standardized. Nowadays we use component composition and Higher-Order Components and they are cool. Let's use them.
## Inspiration
I've lately being coding most of my projects using the Apollo stack. As with any API client, the [Apollo/React](https://github.com/apollographql/react-apollo) integration package provides easy data [request status and meta information](http://dev.apollodata.com/react/queries.html#default-result-props), right in the component as a simple prop. I wanted that when coding simpler projects too, but without API setup hassle :)
## Install
`yarn add react-data-loader`
> Or, good ol' `npm install --save react-data-loader`
## Usage
This lib provides a single Higher-Order Component called `withDataLoader` with the following signature:
```js
withDataLoader(
propName: string,
loader: (props: Object) => Function
): HigherOrderComponent
```
This HoC will inject an object on the property defined by `propName`. The object will have these keys:
key | type | default | description
-----|------|---------|------------
data | `any` | `undefined` | The resulting data. Can be anything you need.
loading | `Boolean` | `false` | Whether or not the loader is currently fetching data.
error | `any` | `null` | The failure result. Can be anything you need (most commonly an `Error` instance).
requests | `Number` | `0` | The amount of times data was fetched using this loader.
promise | `Promise` | `null` | The currently running data fetching promise.
load | `Function` | | The data fetch dispatcher function. Can be called with whatever arguments your `loader` accepts.
The `loader` argument of `withDataLoader` is where the fun happens. It follows a similar specification as that of the [`withHandlers` from Recompose](https://github.com/acdlite/recompose/blob/master/docs/API.md#withhandlers) lib: it must be a Higher-Order Function which will receive `props` and must return a function responsible for fetching data.
### Simple as can
```js
import { withDataLoader } from 'react-data-loader'
const MyDataLoaderComponent = ({ someProp }) => (
{ JSON.stringify(someProp.data) }
Load!
)
// Using Fetch API.
const loader = props => e => fetch('/api/data.json').then(res => res.json())
export default withDataLoader('someProp', loader)(MyDataLoaderComponent)
```
## License
Copyright (c) 2017 Lucas Constantino Silva
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.