react-trashable
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

react-trashable 🚮

A Higher Order Component to make React Components garbage collectable when unmounted.

Learn more about garbage collection and trashable and why you should use it here.

Installation

Using npm:

npm install --save react-trashable

Using yarn:

yarn add react-trashable

How to use

import { withTrashable } from 'react-trashable';

class Component extends React.Component {
    componentDidMount() {
        this.props.registerPromise(apiCall()).then(() => {
            // ...
        }).catch(() => {
            // ...
        });
    }
}

// Passes the registerPromise() function to Component
export default withTrashable(Component);

Gotchas

You need to register the promise before you add your then and catch handlers. Otherwise, you will not get the garbage collection benefits.

// Do this
const registeredPromise = registerPromise(promise);
registeredPromise.then(() => {});

// NOT this
const handledPromise = promise.then(() => {});
registerPromise(handledPromise);

Readme

Keywords

none

Package Sidebar

Install

npm i react-trashable

Weekly Downloads

0

Version

0.1.1

License

MIT

Unpacked Size

45 kB

Total Files

9

Last publish

Collaborators

  • vinceau