All Projects → lucasconstantino → react-data-loader

lucasconstantino / react-data-loader

Licence: MIT license
Dead simple data loader helper for React

Projects that are alternatives of or similar to react-data-loader

YaEtl
Yet Another ETL in PHP
Stars: ✭ 60 (+172.73%)
Mutual labels:  loader
idomizer
An HTML template parser compiling an incremental-dom render factory.
Stars: ✭ 15 (-31.82%)
Mutual labels:  loader
composer-file-loader
Tool to load namespaces and classes from composer.json file without composer
Stars: ✭ 64 (+190.91%)
Mutual labels:  loader
fastify-loader
The route loader for the cool kids!
Stars: ✭ 17 (-22.73%)
Mutual labels:  loader
ots
A widget that can show loaders, notifications, internet connectivity changes as Overlay.
Stars: ✭ 20 (-9.09%)
Mutual labels:  loader
SSSwiftUISpinnerButton
SSSwiftUISpinnerButton is a collection of various spinning animations for buttons in SwiftUI.
Stars: ✭ 37 (+68.18%)
Mutual labels:  loader
vue-load-image
A Vue component for showing loader during image loading https://john015.github.io/vue-load-image/
Stars: ✭ 60 (+172.73%)
Mutual labels:  loader
Solaris
A local LKM rootkit loader/dropper that lists available security mechanisms
Stars: ✭ 47 (+113.64%)
Mutual labels:  loader
vue-skeleton-loader
A simple and easily customizable skeleton loader plugin for you Vue application.
Stars: ✭ 74 (+236.36%)
Mutual labels:  loader
reassemble
Fast Library for the Composition of React Higher-Order-Components
Stars: ✭ 67 (+204.55%)
Mutual labels:  higher-order-component
sw-loader
Yet another service worker loader
Stars: ✭ 19 (-13.64%)
Mutual labels:  loader
shell-loader
A Webpack loader that runs an arbitrary script on matching files
Stars: ✭ 18 (-18.18%)
Mutual labels:  loader
Centrifuge
Cross-platform runtime mod loader and API for any Unity-based game. Supports Unity 5 and up!
Stars: ✭ 27 (+22.73%)
Mutual labels:  loader
react-portalgun
Lightweight portal system for React. Mega seeds included 🔫
Stars: ✭ 75 (+240.91%)
Mutual labels:  higher-order-component
react-sync
A declarative approach to fetching data via a React higher order component
Stars: ✭ 18 (-18.18%)
Mutual labels:  higher-order-component
react-zap
⚡ Zap props from one React component to another, using React new context API and your existing higher-order components ⚡
Stars: ✭ 17 (-22.73%)
Mutual labels:  higher-order-component
proc-that
proc(ess)-that - easy extendable ETL tool for Node.js. Written in TypeScript.
Stars: ✭ 25 (+13.64%)
Mutual labels:  loader
path-replace-loader
Path replace loader for webpack
Stars: ✭ 14 (-36.36%)
Mutual labels:  loader
django-manifest-loader
Simplifies webpack configuration with Django
Stars: ✭ 105 (+377.27%)
Mutual labels:  loader
react-animation-frame
A React higher-order component that invokes a callback in a wrapped component via requestAnimationFrame
Stars: ✭ 47 (+113.64%)
Mutual labels:  higher-order-component

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].