Wondering what’s next for npm?Check out our public roadmap! »

trashable-react

1.1.0 • Public • Published

trashable-react 🚮

npm David CircleCI npm

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

npm install --save trashable-react

How to use

import makeComponentTrashable from 'trashable-react';

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

// Passes the registerPromise() function to Component
export default makeComponentTrashable(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);

Install

npm i trashable-react

DownloadsWeekly Downloads

2,056

Version

1.1.0

License

MIT

Unpacked Size

167 kB

Total Files

11

Last publish

Collaborators

  • avatar