cancel-that-promise

1.0.1 • Public • Published

Cancel that promise

Inspired by this article from React blog.

isMounted is an Antipattern

The primary use case for isMounted() is to avoid calling setState() after a component has unmounted, because calling setState() after a component has unmounted will emit a warning. The “setState warning” exists to help you catch bugs, because calling setState() on an unmounted component is an indication that your app/component has somehow failed to clean up properly. Specifically, calling setState() in an unmounted component means that your app is still holding a reference to the component after the component has been unmounted - which often indicates a memory leak!

Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.

Usage

npm install cancel-that-promise

 
import makeCancelable from 'cancel-that-promise'
 
componentDidMount() {
  this.cancelFetch = makeCancelable(
    fetchResource(this.props.resource),
    fetched => this.setState(fetched),
    error => console.error(error)
  );
}
componentWillUnmount() {
  this.cancelFetch();
}
 

TODO

  • write the README!

Readme

Keywords

none

Package Sidebar

Install

npm i cancel-that-promise

Weekly Downloads

0

Version

1.0.1

License

ISC

Last publish

Collaborators

  • albinotonnina