Coder Social home page Coder Social logo

use-only-change-effect's Introduction

๐Ÿ’ซ use-only-change-effect

You want to use a useEffect every time some value or values are changed but you don't want it to be called on initial render? use-only-change-effect got you covered!

Motivation

React hooks are great, we can use useEffects to run callbacks after the components mounts, when it unmounts and when some values are changed.

But the thing with useEffects that depend on some values in their dependencies array is that they run also after the initial render, even if the value in the dependencies array doesn't change, (well, it "changes" from undefined to its current value).

In the good(?) old days of class components, we had the "componentWillReceiveProps" method that got called on every prop change but not on initial render, but only when new props were received (changed). Many times we want this behavior with hooks too.

So here comes "useOnlyChangeEffect" that will act like the useEffect we all love but will run only when the value actually changes.

Installation

npm install --save use-only-change-effect

or

yarn add use-only-change-effect

Usage

import useOnlyChangeEffect from 'use-only-change-effect';


const MyComponent = ({ value }) => {
    
    // this console.log will run only when the "value" prop changes, and not on initial render too.
    useOnlyChangeEffect(() => {
        console.log('I got called!')
    }, [value])

    return null
}

use-only-change-effect's People

Contributors

benyanir avatar

Watchers

Nir Ben-Yair avatar

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.