Giter Club home page Giter Club logo

react-native-loading-container's Introduction

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, or you can try it from Exponent at exp://exp.host/@brentvatne/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 loading is in progress. 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} />}
/>

react-native-loading-container's People

Contributors

brentvatne avatar

Watchers

James Cloos avatar  avatar

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.