0.1.1 • Published 8 years ago

react-native-cancelable-fetch v0.1.1

Weekly downloads
209
License
MIT
Repository
github
Last release
8 years ago

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