-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathindex.ts
43 lines (40 loc) · 1.72 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
31
32
33
34
35
36
37
38
39
40
41
42
43
import {type DependencyList, useEffect, useRef} from 'react';
import {type DependenciesComparator} from '../types.js';
import {isBrowser} from '../util/const.js';
import {basicDepsComparator, type EffectCallback, type EffectHook} from '../util/misc.js';
/**
* Like `useEffect` but uses provided comparator function to validate 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 comparator Function that compares two dependency arrays,
* and returns `true` if they're equal.
* @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.
*/
// eslint-disable-next-line max-params
export function useCustomCompareEffect<
Callback extends EffectCallback = EffectCallback,
Deps extends DependencyList = DependencyList,
HookRestArgs extends any[] = any[],
R extends HookRestArgs = HookRestArgs,
>(
callback: Callback,
deps: Deps,
comparator: DependenciesComparator<Deps> = basicDepsComparator,
effectHook: EffectHook<Callback, Deps, HookRestArgs> = useEffect,
...effectHookRestArgs: R
): void {
const dependencies = useRef<Deps>();
// Effects are not run during SSR, therefore, it makes no sense to invoke the comparator
if (
dependencies.current === undefined ||
(isBrowser && !comparator(dependencies.current, deps))
) {
dependencies.current = deps;
}
effectHook(callback, dependencies.current, ...effectHookRestArgs);
}