https://github.com/expo/react-native-loading-container
A container component that takes care of loading/catching timeouts/retrying
https://github.com/expo/react-native-loading-container
Last synced: 3 months ago
JSON representation
A container component that takes care of loading/catching timeouts/retrying
- Host: GitHub
- URL: https://github.com/expo/react-native-loading-container
- Owner: expo
- Archived: true
- Created: 2015-12-12T04:09:08.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-05-03T18:27:22.000Z (over 8 years ago)
- Last Synced: 2024-05-02T00:29:16.840Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 82 KB
- Stars: 156
- Watchers: 5
- Forks: 19
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LoadingContainer [](http://slack.expo.io)
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:
```js
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.
```js
export default class ListScreen extends React.Component {render() {
return (
{ /* render content */ }
);
}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](https://github.com/expo/react-native-loading-container/tree/master/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.```js
console.log(e)}
renderLoadingOverlay={props => }
renderErrorOverlay={props => }
/>
```