usetrashable

1.0.1 • Public • Published

useTrashable

A custom hook to cancel unresolved promises when a component unmounts to prevent memory leaks.

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

Installation

npm install --save usetrashable

How to use

import useTrashable from 'usetrashable';

const Component = (props) => {
    const registerPromise = useTrashable();
    registerPromise(apiCall()).then(() => {
            // ...
        }).catch(() => {
            // ...
        });
}

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);

/usetrashable/

    Package Sidebar

    Install

    npm i usetrashable

    Weekly Downloads

    1

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    14.5 kB

    Total Files

    9

    Last publish

    Collaborators

    • petewarman