-
Notifications
You must be signed in to change notification settings - Fork 96
/
index.ts
30 lines (29 loc) · 1.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import {isEqual} from '@react-hookz/deep-equal';
import {type DependencyList, useEffect} from 'react';
import {useCustomCompareEffect} from '../useCustomCompareEffect/index.js';
import {type EffectCallback, type EffectHook} from '../util/misc.js';
/**
* Like `useEffect`, but uses `@react-hookz/deep-equal` comparator function to validate deep
* dependency changes.
*
* @param callback Function that will be passed to the underlying effect hook.
* @param deps Dependency list like the one passed to `useEffect`.
* @param effectHook Effect hook that will be used to run
* `callback`. Must match the type signature of `useEffect`, meaning that the `callback` should be
* placed as the first argument and the dependency list as second.
* @param effectHookRestArgs Extra arguments that are passed to the `effectHook`
* after the `callback` and the dependency list.
*/
export function useDeepCompareEffect<
Callback extends EffectCallback = EffectCallback,
Deps extends DependencyList = DependencyList,
HookRestArgs extends any[] = any[],
R extends HookRestArgs = HookRestArgs,
>(
callback: Callback,
deps: Deps,
effectHook: EffectHook<Callback, Deps, HookRestArgs> = useEffect,
...effectHookRestArgs: R
): void {
useCustomCompareEffect(callback, deps, isEqual, effectHook, ...effectHookRestArgs);
}