Code Monkey home page Code Monkey logo

use-clean-effect's Introduction

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.

use-clean-effect's People

Contributors

ifedapoolarewaju avatar ritschwumm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

ritschwumm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.