Skip to content

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

License

Notifications You must be signed in to change notification settings

ifedapoolarewaju/use-clean-effect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 avoid 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 specifically 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.