Code Monkey home page Code Monkey logo

react-data-loader's Introduction

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:

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

react-data-loader's People

Contributors

lucasconstantino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

rubythonode

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.