npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-data-loader

v0.1.0

Published

Dead simple data loader helper for React

Downloads

1

Readme

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