react-native-cancelable-fetch

0.1.1 • Public • Published

react-native-cancelable-fetch

Build Status Coverage Status npm version

Cancelable fetch within a react native app

Installation

npm i --save react-native-cancelable-fetch

Usage

const fetch = require('react-native-cancelable-fetch');
 
// Make fetch request
fetch('http://localhost/', null, 1)
  .then(res => res.json())
  .then(data => {
    console.log(data);
  });
 
// Cancel request
fetch.abort(1);
 

Use object for tag

const fetch = require('react-native-cancelable-fetch');
...
 
const Movies = React.createClass({
  componentDidMount() {
    // fetch movies
    fetch(REQUEST_URL, null, this)
      .then((response) => response.json())
      .then((response) => this.setState({
        dataSource: this.state.dataSource.cloneWithRows(response.movies),
        loaded: true,
      }));
  },
  componentWillUnmount() {
    // Cancel request
    fetch.abort(this);
  },
  ...
});
 

API

  1. fetch(input, init, tag) make a request with a tag (tag can be number, string or object)
  2. fetch.abort(tag) cancel request by tag

Package Sidebar

Install

npm i react-native-cancelable-fetch

Weekly Downloads

59

Version

0.1.1

License

MIT

Last publish

Collaborators

  • apentle