useFreshRef
Returns a ref with the latest and fresh value passed to it.
The useFreshRef
hook is helpful when you want to address the issue of stale closures by providing a ref with the latest and freshest value passed to it.
It can be particularly helpful to prevent stale state in callbacks, ensuring that the latest value is always accessible without introducing unnecessary re-renders.
useFreshRef
Count Value: 0
Usage
Import the hook from @abhushanaj/react-hooks
and use in required component.
API Reference
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
value | T | The value which needs to be fresh at all times. | N/A |
Return Value
Parameter | Type | Description | Default |
---|---|---|---|
ref | React.RefObject<T> | Ref object with the latest and fresh value. | N/A |
Note: The useFreshRef
is probably the most useful when used with callback functions where you want to abstract reactive but non synchronizing values.