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

use-clean-effect

v0.1.5

Published

An extension of React useEffect without the need for clean-up functions

Downloads

2

Readme

use-clean-effect

An extended version of React useEffect that is built-in with a simple clean-up function, hence you may not need to add your own clean-up function when using this.

It also passes an argument to the useEffect callback that indicates if the component has been unmounted or re-rendered. Therefore, making it easy to invoke async functions in your useEffect callback and avoid memory leaks and removing the need for you to add a clean-up function.

Installation

npm install use-clean-effect

or

yarn add use-clean-effect

Usage

useCleanEffect strives to preserve the usage interface of React's useEffect. You can essentially swap useCleanEffect with React's useEffect without any change in your codebase.

However, if you want to leverage on the extra feature offered by useCleanEffect you can access the extra argument passed to the useCleanEffect callback like so:

import { useCleanEffect } from 'use-clean-effect'

useCleanEffect((phase) => {
    // an asynchronous call
    someHttpRequest().then((data) => {
        if (!phase.active) {
            // component has been unmounted/re-rendered so we abort to avouid memory-leak
            return
        }

        // go ahead to use data since then component has not been unmounted/re-rendered
        ...
    })
}, [])

The phase argument is an object that contains a boolean field active which has the value true if the component hasn't been unmounted or re-rendered since the useEffect callback was triggered. And is false otherwise (in which case, we should abort the function to avoid memory leak).

Additional clean-up Function

For most cases you won't need to add a clean up function to your useCleanEffect callback, since it implicitly handles the clean-up logic. However, if your use-case requires that you spcecifically handle a clean-up logic, you can still return your clean-up function the way you would do it for React's useEffect.

import { useCleanEffect } from 'use-clean-effect'

useCleanEffect((phase) => {
    // an async call
    someHttpRequest().then((data) => {
        // ...
    })

    const customCleanUpFunction = () => {
        // custom clean-up logic here
    }

    return customCleanUpFunction
}, [])

If you would like to swap all occurrences of React's useEffect with useCleanEffect without much modification, you can simply import useCleanEffect using the as keyword like so:

import { useCleanEffect as useEffect } from 'use-clean-effect'
// and then delete occurrences of `import { useEffect } from 'react'`

License

The MIT License.