Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/useDeepCompareEffect.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const Demo = () => {
};
```

## When to Use

This hook is handy when:
- Your dependencies might be `undefined` or an object (common with optional props/config)
- You need to compare objects by their values, not just references
- You're mixing primitives and objects in your dependency array

Works fine with any dependency types - primitives, objects, or a mix of both.

## Reference

```ts
Expand Down
16 changes: 0 additions & 16 deletions src/useDeepCompareEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,7 @@ import { DependencyList, EffectCallback } from 'react';
import useCustomCompareEffect from './useCustomCompareEffect';
import isDeepEqual from './misc/isDeepEqual';

const isPrimitive = (val: any) => val !== Object(val);

const useDeepCompareEffect = (effect: EffectCallback, deps: DependencyList) => {
if (process.env.NODE_ENV !== 'production') {
if (!(deps instanceof Array) || !deps.length) {
console.warn(
'`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead.'
);
}

if (deps.every(isPrimitive)) {
console.warn(
'`useDeepCompareEffect` should not be used with dependencies that are all primitive values. Use React.useEffect instead.'
);
}
}

useCustomCompareEffect(effect, deps, isDeepEqual);
};

Expand Down