🤙The same useRef, but it will callback
Installations
npm install use-callback-ref
Releases
Unable to fetch releases
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
>=10
Typescript Support
Yes
Node Version
18.19.1
NPM Version
10.2.4
Statistics
297 Stars
51 Commits
5 Forks
2 Watching
17 Branches
4 Contributors
Updated on 21 Nov 2024
Bundle Size
1.78 kB
Minified
684.00 B
Minified + Gzipped
Languages
TypeScript (91.5%)
JavaScript (7.81%)
HTML (0.69%)
Total Downloads
Cumulative downloads
Total Downloads
652,013,482
Last day
-1.1%
1,330,583
Compared to previous day
Last week
4.1%
6,965,029
Compared to previous week
Last month
11.1%
28,943,910
Compared to previous month
Last year
108.1%
304,665,937
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
2
🤙 use-callback-ref 📞
The same `useRef` but it will callback: 📞 Hello! Your ref was changed!
Keep in mind that useRef doesn't notify you when its content changes. Mutating the .current property doesn't cause a re-render. If you want to run some code when React attaches or detaches a ref to a DOM node, you may want to use
a callback ref instead.... useCallbackRef instead.
Read more about use-callback
pattern and use cases:
This library exposes helpers to handle any case related to ref
lifecycle
useCallbackRef
- react on a ref change (replacement foruseRef
)createCallbackRef
- - low level version ofuseCallbackRef
useMergeRefs
- merge multiple refs together creating a stable return refmergeRefs
- low level version ofuseMergeRefs
useTransformRef
- transform one ref to another (replacement foruseImperativeHandle
)transformRef
- low level version ofuseTransformRef
useRefToCallback
- convert RefObject to an old callback-style refrefToCallback
- low level version ofuseRefToCallback
assignRef
- assign value to the ref, regardless it is RefCallback or RefObject
All functions are tree shakable, but even together it's less then 300b.
API
💡 Some commands are hooks based, and returns the same refs/functions every render. But some are not, to be used in classes or non-react code.
useRef API
🤔 Use case: every time you have to react to ref change
API is 99% compatible with React createRef
and useRef
, and just adds another argument - callback
,
which would be called on ref update.
createCallbackRef - to replace React.createRef
createCallbackRef(callback)
- would call providedcallback
when ref is changed.
useCallbackRef - to replace React.useRef
useCallbackRef(initialValue, callback)
- would call providedcallback
when ref is changed.
callback
in both cases iscallback(newValue, oldValue)
. Callback would not be called if newValue and oldValue is the same.
1import { useRef, createRef, useState } from 'react'; 2import { useCallbackRef, createCallbackRef } from 'use-callback-ref'; 3 4const Component = () => { 5 const [, forceUpdate] = useState(); 6 // I dont need callback when ref changes 7 const ref = useRef(null); 8 9 // but sometimes - it could be what you need 10 const anotherRef = useCallbackRef(null, () => forceUpdate()); 11 12 useEffect(() => { 13 // now it's just possible 14 }, [anotherRef.current]); // react to dom node change 15};
💡 You can use useCallbackRef
to convert RefObject into RefCallback, creating bridges between the old and the new code
1// some old component 2const onRefUpdate = (newRef) => {...} 3const refObject = useCallbackRef(null, onRefUpdate); 4// ... 5<SomeNewComponent ref={refObject}/>
assignRef
🤔 Use case: every time you need to assign ref manually, and you dont know the shape of the ref
assignRef(ref, value)
- assigns values
to the ref
. ref
could be RefObject or RefCallback.
🚫 ref.current = value // what if it's a callback-ref?
🚫 ref(value) // but what if it's a object ref?
import {assignRef} from "use-callback-ref";
✅ assignRef(ref, value);
useTransformRef (to replace React.useImperativeHandle)
🤔 Use case: ref could be different.
transformRef(ref, tranformer):Ref
- return a new ref
which would propagate all changes to the provided ref
with applied transform
1// before 2const ResizableWithRef = forwardRef((props, ref) => <Resizable {...props} ref={(i) => i && ref(i.resizable)} />); 3 4// after 5 6const ResizableWithRef = forwardRef((props, ref) => ( 7 <Resizable {...props} ref={transformRef(ref, (i) => (i ? i.resizable : null))} /> 8));
refToCallback
refToCallback(ref: RefObject): RefCallback
- for compatibility between the old and the new code.
For the compatibility between RefCallback
and RefObject use useCallbackRef(undefined, callback)
useMergeRefs
mergeRefs(refs: arrayOfRefs, [defaultValue]):ReactMutableRef
- merges a few refs together
When developing low level UI components, it is common to have to use a local ref but also support an external one using React.forwardRef. Natively, React does not offer a way to set two refs inside the ref property. This is the goal of this small utility.
1import React from 'react'; 2import { useMergeRefs } from 'use-callback-ref'; 3 4const MergedComponent = React.forwardRef((props, ref) => { 5 const localRef = React.useRef(); 6 // ... 7 // both localRef and ref would be populated with the `ref` to a `div` 8 return <div ref={useMergeRefs([localRef, ref])} />; 9});
💡 - useMergeRefs
will always give you the same return, and you don't have to worry about [localRef, ref]
unique every render.
mergeRefs
mergeRefs(refs: arrayOfRefs, [defaultValue]):ReactMutableRef
- merges a few refs together
is a non-hook based version. Will produce the new ref
every run, causing the old one to unmount, and be populated with the null
value.
mergeRefs are based on https://github.com/smooth-code/react-merge-refs, just exposes a RefObject, instead of a callback
mergeRefs
are "safe" to use as a part of other hooks-based commands, but don't forget - it returns a new object every call.
Similar packages:
- apply-ref -
applyRefs
is simular tomergeRef
,applyRef
is similar toassignRef
- useForkRef -
useForkRef
is simular touseMergeRefs
, but accepts only two arguments. - react-merge-refs -
merge-refs
is simular touseMergeRefs
, but not a hook and does not provide "stable" reference.
Is it a rocket science? No,
RefObject
is no more than{current: ref}
, anduse-callback-ref
is no more thangetter
andsetter
on that field.
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 3/24 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Reason
48 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-phwq-j96m-2c2q
- Warn: Project is vulnerable to: GHSA-ghr5-ch3p-vcr6
- Warn: Project is vulnerable to: GHSA-vh7m-p724-62c2
- Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-vx3p-948g-6vhq
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.9
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More