Skip to content

apentle/react-native-cancelable-fetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Cancelable fetch within a react native app

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published