@utilityjs/use-get-latest
Version:
A React hook that stores & updates `ref.current` with the most recent value.
53 lines (37 loc) • 1.48 kB
Markdown
<div align="center">
<h1 align="center">
useGetLatest
</h1>
</div>
<div align="center">
A React hook that stores & updates `ref.current` with the most recent value.
[](https://github.com/mimshins/utilityjs/blob/main/LICENSE)
[](https://www.npmjs.com/package/@utilityjs/use-get-latest)
[](https://www.npmjs.com/package/@utilityjs/use-get-latest)
[](https://www.npmjs.com/package/@utilityjs/use-get-latest)
```bash
npm i @utilityjs/use-get-latest | yarn add @utilityjs/use-get-latest
```
</div>
<hr>
## Usage
```jsx
import useGetLatest from "@utilityjs/use-get-latest";
import * as React from "react";
const useAttachDomClick = (callback) => {
const cachedCallback = useGetLatest(callback);
React.useEffect(() => {
document.addEventListener("click", cachedCallback.current);
return () => {
document.removeEventListener("click", cachedCallback.current);
}
}, [])
};
```
## API
### `useGetLatest(value)`
```ts
declare const useGetLatest: <T>(value: T) => MutableRefObject<T>;
```
#### `value`
The value to be stored.