https://github.com/termosa/use-last-ref
Constantly updating ref value from the argument
https://github.com/termosa/use-last-ref
Last synced: about 1 year ago
JSON representation
Constantly updating ref value from the argument
- Host: GitHub
- URL: https://github.com/termosa/use-last-ref
- Owner: termosa
- Created: 2023-07-22T16:03:00.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-07-22T16:03:36.000Z (almost 3 years ago)
- Last Synced: 2025-02-10T12:43:55.421Z (over 1 year ago)
- Language: JavaScript
- Size: 430 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# use-last-ref
> Constantly updating ref value from the argument
[](https://www.npmjs.com/package/use-last-ref) [](https://standardjs.com)
## Install
```bash
npm install --save use-last-ref
```
## Usage
```tsx
import * as React from 'react'
import api from "./api"
import useLastRef from 'use-last-ref'
// Or: import { useLastRef } from 'use-last-ref'
const ExampleInput = ({ value }: { value?: string }) => {
const valueRef = useLastRef(value)
const save = React.useCallback((newValue: string) => {
// Instead of adding `[value]` to `useCallback` dependency list
// and updating `save` function and `` component
// we use React reference feature to always access updated value
// Prevent submitting the save value
if (newValue !== valueRef.current) {
api.saveNewValue(newValue)
}
}, [])
return
}
```
## License
MIT © [termosa](https://github.com/termosa)
---
This hook is created using [create-react-hook](https://github.com/hermanya/create-react-hook).