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-native-loading-container

v0.5.0

Published

LoadingContainer is a component that co-ordinates fetching data, displaying a loading indicator while that's in progress, and an error message with a retry button when a request fails.

Downloads

32

Readme

LoadingContainer Slack

LoadingContainer is a component that co-ordinates fetching data, displaying a loading indicator while that's in progress, and an error message with a retry button when a request fails.

Why?

We are building an app where pretty much every screen has to fetch new data, so we wanted a way to generalize our logic for showing loading screens, handling errors and retries.

Installation

npm install react-native-loading-container --save

LoadingContainer is compatible with React Native 0.15 and newer.

Usage

Import LoadingContainer as a JavaScript module:

import LoadingContainer from 'react-native-loading-container';

The only two props that are required are onLoadStartAsync and onReadyAsync - these must both be async functions (or return Promises), and must resolve when they are completed.

onLoadStartAsync is responsible for fetching the data that the scene needs to render and returning it. In the example below, we return the JSON response from the reactnative subreddit. If an exception is thrown in this function, an error overlay is rendered where the user can tap to retry. Tapping retry will invoke this function again. The return value of onLoadStartAsync is fed into _onReadyAsync.

onReadyAsync is responsible for taking the data fetched by onLoadStartAsync and updating our component state. When it resolves, LoadingContainer will hide the loading indicator.

export default class ListScreen extends React.Component {

  render() {
    return (
      <LoadingContainer
        onLoadStartAsync={this._loadInitialDataAsync.bind(this)}
        onReadyAsync={this._onReadyAsync.bind(this)}>
        { /* render content */ }
      </LoadingContainer>
    );
  }

  async _loadInitialDataAsync() {
    let response = await fetchWithTimeout('https://www.reddit.com/r/reactnative.json');
    return response.json();
  }

  async _onReadyAsync({data: {children: stories}}) {
    return new Promise((resolve) => {
      this.setState({stories}, resolve);
    });
  }

}

A complete, runnable example is available in Examples/StarterTemplate.

LoadingContainer can be used anywhere, but is especially well-suited to wrapping the content of your scene components. A scene component is the top-level component for each route.

Customization

LoadingContainer was built to our specific use case so it might not have all of the hooks that you will want to customize it for your application -- if this is the case, please submit a pull request.

Currently only the following props are exposed:

  • onError - invoked with the exception object when onLoadStartAsync throws an exception.
  • renderLoadingOverlay - returns a React element that will be rendered when loading is in progress. It must implement showOverlay, hideOverlay and fadeOverlay methods.
  • renderErrorOverlay - returns a React element that will be rendered when error occurs. It will receive a function prop called onRetryLoad that should be invoked when the user indicates that they would like retry fetching data.
<LoadingContainer
  onError={e => console.log(e)}
  renderLoadingOverlay={props => <MyCustomLoadingOverlay {...props} />}
  renderErrorOverlay={props => <MyCustomErrorOverlay {...props} />}
/>