0.1.0 • Published 7 years ago

react-data-loader v0.1.0

Weekly downloads
13
License
MIT
Repository
github
Last release
7 years ago

React Data Loader Build Status

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 (I personally recommend Apollo for that task) or even Redux with 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 integration package provides easy data request status and meta information, 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:

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:

keytypedefaultdescription
dataanyundefinedThe resulting data. Can be anything you need.
loadingBooleanfalseWhether or not the loader is currently fetching data.
erroranynullThe failure result. Can be anything you need (most commonly an Error instance).
requestsNumber0The amount of times data was fetched using this loader.
promisePromisenullThe currently running data fetching promise.
loadFunctionThe 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 the same specification as the withHandlers from Recompose lib: it must be a Higher-Order Function which will receive props and must return a function responsible for fetching data.

Simple as can

import { withDataLoader } from 'react-data-loader'

const MyDataLoaderComponent = ({ someProp }) => (
  <div>
    { JSON.stringify(someProp.data) }
    <button onClick={ someProp.load } disabled={ someProp.loading }>Load!</button>
  </div>
)

// 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.