npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-disposable-decorator

v4.0.2

Published

decorator for handling observables

Downloads

337

Readme

react-disposable-decorator

Decorator for automatically canceling observable and other subscriptions when a React component is unmounted.

A cancelWhenUnmounted function is passed to the decorated component as a prop, which should be called with an observable subscription (a disposable). Click here for more documentation on canceling observables.

Also, a cancelAllSubscriptions function is passed to the decorated component as a prop. This should be called with no arguments, and will cancel all subscriptions that were registered via cancelWhenUnmounted. It will also reset the list of subscriptions to be empty, so that future calls to cancelWhenUnmounted and cancelAllSubscriptions will start fresh. Note that cancelAllSubscriptions is automatically called during componentWillUnmount.

Finally, react-disposable-decorator uses the forwardRef API (requires 16.3+) so it is possible to access the original component.

Installation

npm install react-disposable-decorator or yarn add react-disposable-decorator

Usage

import Cancelable from 'react-disposable-decorator';

@Cancelable //decorate the component
export default class SomeComponent extends React.Component {
  componentDidMount() {
    this.props.cancelWhenUnmounted(
      /* This will be automatically canceled if the observable does not
	   * onComplete or onError before the React component is unmounted.
	   */
      fetchSomeData.subscribe( data => this.setstate({data}) )
    );
  }
  componentDidUpdate(prevProps) {
    // Example usage of how you might use cancelAllSubscriptions
    if (this.props.needToMakeNewSubscriptions) {
      this.props.cancelAllSubscriptions();
      this.props.cancelWhenUnmounted(
        fetchSomeData.subscribe(data => this.setState({data}));
      );
    }
  }
}
...

In tests

If you're using Enzyme shallow rendering, try doing shallow(<Foo />).dive() on components decorated with react-disposable-decorator to be able to test the wrapped component.

If you wish to disable the react-disposable-decorator in tests altogether (so you don't have to .dive()), you can set a global variable global.disableReactDisposableDecorator = true in your test configuration before you import the react-disposable-decorator javascript module into the test environment.

Refs

react-disposable-decorator uses react 16.3+ and the forwardRef API to ensure that your refs go to the original component and not the decorated component. Using refs to access components have good use cases listed here.

import Cancelable from 'react-disposable-decorator'

@Cancelable
class Hello extends React.Component {
  constructor(props) {
    super(props)
      this.logHello = this.logHello.bind(this)
  }
  logHello() {
    console.log('hello')
  }
  render () {
    <div>
      Hello
    </div>
  }
}

class wrappingComponent extends React.Component {
  componentDidMount() {
    this.hello.logHello()
    // calls logHello
  }
  render () {
    return (
      <div>
        <Hello ref={el => this.hello = el}>
      </div>
    )
  }
}