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-remote-data-js

v0.1.0

Published

A React component around [remote-data-js](https://github.com/jackfranklin/remote-data-js), a JavaScript library aimed at modelling remote data and the states it can be in. This library provides a `<RemoteData />` component to deal with fetching and displa

Downloads

6

Readme

react-remote-data

A React component around remote-data-js, a JavaScript library aimed at modelling remote data and the states it can be in. This library provides a <RemoteData /> component to deal with fetching and displaying remote data or the error state.

npm install --save react-remote-data-js

There is also a build that will put this library in the global scope as window.ReactRemoteData. I recommend only using this for testing purposes - bundling via Browserify/Webpack/Rollup/etc is much better for proper application development.

Usage

import RemoteData from 'react-remote-data-js'

<RemoteData url="http://example.com/users/jack"
  notAsked={props => <div><button onClick={props.fetch}>Make Request</button></div>}
  pending={() => <p>Loading...</p>}
  success={props => <div><UserComponent user={props.request.data} /></div>}
  failure={props => <div><ApiErrorComponent error={props.request.data} /></div>}
/>

You can also see an example on JSBin and get code running immediately.

Render a RemoteData component and pass it the following props:

  • url: String | Function the URL that the request will be made to.

Render a RemoteData component and pass it the following props:

  • url: String | Function the URL that the request will be made to. If you give a function, it will be called with arguments that you give to the fetch function (see below for an example)
  • notAsked: Function a function that takes properties given to it by the <RemoteDataJs> component and returns what will be rendered when the request is in the NotAsked state.
  • pending, success and failure are all the same as notAsked but are used for the relevant state.

When notAsked, pending, success and failure are rendered, the function you provide is called with some props. They are:

  • fetch: Function call props.fetch(...args) to start the request. This immediately turns the request to pending, and it will update to success or failure depending on the outcome of the request.
  • request: RemoteDataJs this is the underlying instance of the RemoteDataJs object that this component wraps around. The bit you'll most likely be interested in is request.data, which contains the API response for a successful request, or an Error for a failed request.

Fetching URLs with parameters

Often you'll want to take a piece of data and use it to construct a URL. For example, you might have a form that lets a user type in a username to search your API for. In that case you need to define the url property as a function. You'll then need to update your call to props.fetch to pass in the username:

<RemoteData url={name => `http://example.com/users/${name}`}
  notAsked={props => <div><button onClick={() => props.fetch(this.state.userName)}>Make Request</button></div>}
  pending={() => <p>Loading...</p>}
  success={props => <div><UserComponent user={props.request.data} /></div>}
  failure={props => <div><ApiErrorComponent error={props.request.data} /></div>}
/>

Abstracting

You might have a common <Loading> component that you want to always use. In that case I recommend a simple wrapper component around <RemoteData> like so:

import Loading from './components/loading'

const MyRemoteData = props => (
  <RemoteData pending={() => <Loading />} {...props} />
)

You can do this with any of the properties that <RemoteData> expects.

Questions, problems, comments?

Would love to hear them! Please raise an issue on this repository.