use-actual-callback
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

use-actual-callback

Installation

$ yarn add use-actual-callback

Why?

useCallback is over complicated and useless in some cases:

  • When your function depends on many external variables and props, the reference returned from useCallback changes often and triggers undesired renders and effects.
  • You always need to pass dependencies. Sometimes it can be a really big array. Of course, you need to avoid such situations, but in some cases it's hard to do.

useActualCallback is the better version of useCallback:

  • When you're accessing the external variables inside the callback, you always get the actual value. It's possible because useActualCallback wraps your function in the other one. Your function is always the last passed inside, but the wrapper has the stable reference, that doesn't change at all.
  • The returned function is always the same.
  • You don't need to pass dependencies array. Never.

Example

import { useActualCallback } from 'use-actual-callback'

const MyComponent = () => {
  const [one, setOne] = useState(/* ... */)
  const [two, setTwo] = useState(/* ... */)
  const [three, setThree] = useState(/* ... */)

  const doSomething = useActualCallback(() => {
    // one, two and three are always actual
    console.log({ one, two, three })
  })
}

Readme

Keywords

none

Package Sidebar

Install

npm i use-actual-callback

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

4.19 kB

Total Files

6

Last publish

Collaborators

  • risenforces