use-deep-compare
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -8,27 +8,22 @@ 'use strict'; | ||
var React = _interopDefault(require('react')); | ||
var deepEqual = _interopDefault(require('dequal')); | ||
var dequal = require('dequal'); | ||
function checkDeps(deps, name) { | ||
const reactHookName = `React.${name.replace(/DeepCompare/, "")}`; | ||
function useDeepCompareMemoize(dependencies) { | ||
const dependenciesRef = React.useRef(dependencies); | ||
const signalRef = React.useRef(0); | ||
if (!deps || deps.length === 0) { | ||
throw new Error(`${name} should not be used with no dependencies. Use ${reactHookName} instead.`); | ||
if (!dequal.dequal(dependencies, dependenciesRef.current)) { | ||
dependenciesRef.current = dependencies; | ||
signalRef.current += 1; | ||
} | ||
} | ||
function useDeepCompareMemoize(value) { | ||
const ref = React.useRef([]); | ||
if (!deepEqual(value, ref.current)) { | ||
ref.current = value; | ||
} | ||
return ref.current; | ||
return React.useMemo(() => dependenciesRef.current, [signalRef.current]); | ||
} | ||
/** | ||
* `useDeepCompareEffect` will return a memoized version of the callback that | ||
* only changes if one of the `deps` has changed. | ||
* `useDeepCompareCallback` will return a memoized version of the callback that | ||
* only changes if one of the `dependencies` has changed. | ||
* | ||
* Usage note: only use this if `deps` are objects or arrays that contain | ||
* objects. Otherwise you should just use React.useEffect. | ||
* Warning: `useDeepCompareCallback` should not be used with dependencies that | ||
* are all primitive values. Use `React.useCallback` instead. | ||
* | ||
@@ -38,6 +33,2 @@ */ | ||
function useDeepCompareCallback(callback, dependencies) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
checkDeps(dependencies, 'useDeepCompareCallback'); | ||
} | ||
return React.useCallback(callback, useDeepCompareMemoize(dependencies)); | ||
@@ -47,19 +38,10 @@ } | ||
/** | ||
* `useDeepCompareEffect` Accepts a function that contains imperative, possibly | ||
* effectful code. | ||
* Accepts a function that contains imperative, possibly effectful code. | ||
* | ||
* @param effect Imperative function that can return a cleanup function | ||
* @param deps If present, effect will only activate if the values in the list | ||
* change. | ||
* Warning: `useDeepCompareEffect` should not be used with dependencies that | ||
* are all primitive values. Use `React.useEffect` instead. | ||
* | ||
* Usage note: only use this if `deps` are objects or arrays that contain | ||
* objects. Otherwise you should just use React.useEffect. | ||
* | ||
*/ | ||
function useDeepCompareEffect(effect, dependencies) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
checkDeps(dependencies, 'useDeepCompareEffect'); | ||
} | ||
React.useEffect(effect, useDeepCompareMemoize(dependencies)); | ||
@@ -70,6 +52,6 @@ } | ||
* `useDeepCompareMemo` will only recompute the memoized value when one of the | ||
* `deps` has changed. | ||
* `dependencies` has changed. | ||
* | ||
* Usage note: only use this if `deps` are objects or arrays that contain | ||
* objects. Otherwise you should just use React.useMemo. | ||
* Warning: `useDeepCompareMemo` should not be used with dependencies that | ||
* are all primitive values. Use `React.useMemo` instead. | ||
* | ||
@@ -79,6 +61,2 @@ */ | ||
function useDeepCompareMemo(factory, dependencies) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
checkDeps(dependencies, 'useDeepCompareMemo'); | ||
} | ||
return React.useMemo(factory, useDeepCompareMemoize(dependencies)); | ||
@@ -85,0 +63,0 @@ } |
{ | ||
"name": "use-deep-compare", | ||
"description": "React hooks, except using deep comparison on the inputs, not reference equality", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"license": "MIT", | ||
@@ -17,3 +17,3 @@ "files": [ | ||
"dependencies": { | ||
"dequal": "1.0.0" | ||
"dequal": "2.0.3" | ||
}, | ||
@@ -24,4 +24,4 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/preset-react": "7.10.1", | ||
"@babel/preset-typescript": "7.10.1", | ||
"@babel/preset-react": "7.23.3", | ||
"@babel/preset-typescript": "7.23.3", | ||
"@pika/pack": "0.5.0", | ||
@@ -32,6 +32,5 @@ "@pika/plugin-build-node": "0.9.2", | ||
"@pika/plugin-standard-pkg": "0.9.2", | ||
"@types/react": "16.9.35", | ||
"react": "16.13.1", | ||
"react-dom": "16.13.1", | ||
"typescript": "3.9.3" | ||
"@types/react": "18.2.45", | ||
"react": "18.2.0", | ||
"typescript": "5.3.3" | ||
}, | ||
@@ -38,0 +37,0 @@ "esnext": "dist-src/index.js", |
@@ -76,4 +76,21 @@ # Use Deep Compare | ||
### react-hooks/exhaustive-deps eslint warnings | ||
To receive eslint warnings about missing array dependencies, just like for standard `useEffect`, `useCallback`, `useMemo` hooks - extend you eslint config as follows: | ||
```json | ||
{ | ||
"rules": { | ||
// ... | ||
"react-hooks/exhaustive-deps": ["warn", { | ||
"additionalHooks": "(useDeepCompareEffect|useDeepCompareCallback|useDeepCompareMemo)" | ||
}] | ||
} | ||
} | ||
``` | ||
## Credits | ||
- Inspired by [use-deep-compare-effect](https://github.com/kentcdodds/use-deep-compare-effect). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
10
96
0
6134
4
50
1
+ Addeddequal@2.0.3(transitive)
- Removeddequal@1.0.0(transitive)
Updateddequal@2.0.3