https://github.com/discord/use-memo-value
Reuse the previous version of a value unless it has changed
https://github.com/discord/use-memo-value
memoize react react-hooks
Last synced: about 1 year ago
JSON representation
Reuse the previous version of a value unless it has changed
- Host: GitHub
- URL: https://github.com/discord/use-memo-value
- Owner: discord
- License: mit
- Created: 2020-04-21T00:43:26.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-04T14:42:36.000Z (over 2 years ago)
- Last Synced: 2025-03-30T00:11:04.176Z (about 1 year ago)
- Topics: memoize, react, react-hooks
- Language: TypeScript
- Size: 489 KB
- Stars: 168
- Watchers: 9
- Forks: 11
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `useMemoValue()`
> Reuse the previous version of a value unless it has changed
## Install
```sh
npm install --save-dev use-memo-value
```
## Usage
If you don't know all the members of an object, you may want to use a "shallow
compare" to memoize the value so you can rely on React's referential equality
(such as in `useEffect(..., deps)`).
```js
import useMemoValue from "use-memo-value"
function MyComponent(props) {
let rawParams = getCurrentUrlQueryParams() // we don't know the shape of this object
let memoizedParams = useMemoValue(rawParams)
useEffect(() => {
search(memoizedParams)
}, [memoizedParams])
// ...
}
```
> **Note:** If you know the shape of your object, you are likely better off not
> using this library.
If you need to customize how the values are compared, you can pass a comparator
as a second argument:
```js
let memoizedValue = useMemoValue(rawValue, (nextValue, previousValue) => {
return Object.is(a, b) // or whatever
})
```
The comparator will not be called until there's a new value.