`usePull` is a hook that will wrap a value with a ref and return a memoized callback that pulls the latest version of that value.
License: MIT License
JavaScript 77.92%TypeScript 22.08%
use-pull's Introduction
usePull ·
usePull is a hook that will wrap a value with a ref and return a memoized callback that pulls the latest version of that value.
Installation
npm install --save use-pull
Usage
importReactfrom'react';importusePullfrom'use-pull';functionExampleComponent({ id }){constvalue=/* ... */;// wrap your value with `usePull`constgetValue=usePull(value);// `usePull` will then return a memoized callback// that won't change references between renders// there are many reasons you'd want to do this:// - one reason is to preserve the reference of a callback by// pulling the latest value at the time the callback is ranconsthandleSomething=useCallback(()=>{constvalue=getValue();},[getValue]);// - another reason may be to pull a value in an effect// but not react to changes in that valueuseEffect(()=>{constvalue=getValue();// do something with `value` and `id`...},[id,getValue]);// ...}