Skip to content
Reuse the previous version of a value unless it has changed
TypeScript
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Make safe in concurrent mode Apr 21, 2020
.gitignore Init commit Apr 21, 2020
.prettierignore Init commit Apr 21, 2020
.prettierrc Init commit Apr 21, 2020
LICENSE Init commit Apr 21, 2020
README.md Init commit Apr 21, 2020
package-lock.json 1.0.1 Apr 21, 2020
package.json --inline none Apr 28, 2020
tsconfig.json Init commit Apr 21, 2020
tsconfig.test.json Init commit Apr 21, 2020

README.md

useMemoValue()

Reuse the previous version of a value unless it has changed

Install

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)).

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:

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.

You can’t perform that action at this time.