Giter Site home page Giter Site logo

use-ref-deps-effect's Introduction

use-ref-deps-effect

npm version

Effect hooks that support "ref.current" dependencies

Motivation

ref.current for DOM Element is null until the rendering is over. So it can not be used as a dependency of useEffect.

useEffect(() => {
    /* ... */
}, [ref.current]) // <= not working !

There are some known alternatives, but they are not familiar with the react mental model. use-ref-deps-effect provides a mental model similar to useEffect by lazy evaluation of ref.current dependencies.

useRefDepsEffect(() => {
    /* ... */
}, [ref]) // <= If ref.current changes, it runs.

Install

npm i use-ref-deps-effect

API

useRefDepsEffect(callback, deps)

import { useRefDepsEffect } from 'use-ref-deps-effect'

This is an Effect hook that supports ref.current dependencies.

createRefDepsHook(useEffectLike)

This function uses the same API hook as the useEffect to create a hook that supports ref.current.

import { createRefDepsHook } from 'use-ref-deps-effect'
import { useLayoutEffect } from 'react'

const useRefDepsLayoutEffect = createRefDepsHook(useLayoutEffect)

Caution

Do not cleanup using ref.current directly.

ref.current is always latest. To do cleanup, you need to capture the previous value in advance.

// ๐Ÿ™… Bad
useRefDepsEffect(() => {
    ref.current.addEventListener(/* ... */)
    return () => {
        ref.current.removeEventListener(/* ... */)
    }
}, [ref])

// โœ… Good
useRefDepsEffect(() => {
    const el = ref.current
    el.addEventListener(/* ... */)
    return () => {
        el.removeEventListener(/* ... */)
    }
}, [ref])

License

MIT

use-ref-deps-effect's People

Contributors

skt-t1-byungi avatar lifeisbeautifu1 avatar

Stargazers

Valery avatar  avatar Roman avatar Andrejs Agejevs avatar  avatar  avatar Eva1ent avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

lifeisbeautifu1

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.